Hello

I have seen the following method syntax and I am trying to understand how this works/or doesn't:

public class MyClass
    {

       public MyClass();

        public MyClass(string param);
  }

There is no implementation of the methods (constructors) and class is not declared virtual either...
Is this something new in C# 3.0 ???

Any ideas???

Thank you

Recommended Answers

All 6 Replies

That doesn't compile in 3.5 and I think the error message answers your question:

'daniweb.MyClass.MyClass()' must declare a body because it is not marked abstract, extern, or partial
'daniweb.MyClass.MyClass(string)' must declare a body because it is not marked abstract, extern, or partial

Interfaces allow you to declare methods similarly but the method must have a return type:

public interface ITest
  {
    void ITest();
  }

I suspect you were looking at partial class definitions.

Either that or the methods were abstract.

Either that or the methods were abstract.

You can't have abstract constructors since the child classes cannot implement constructors from an inherited class...

None of these are valid:

public abstract class AbstractClass
  {
    abstract AbstractClass();
  }
  public abstract class AbstractClass2
  {
    AbstractClass2();
  }
  public class AbstractClass3
  {
    abstract AbstractClass3();
  }

You can't have abstract constructors since the child classes cannot implement constructors from an inherited class...

Obviously I wasn't talking about his code example because the methods there were not marked abstract.

Hello

I have seen the following method syntax and I am trying to understand how this works/or doesn't:

public class MyClass
    {

       public MyClass();

        public MyClass(string param);
  }

There is no implementation of the methods (constructors) and class is not declared virtual either...
Is this something new in C# 3.0 ???

Any ideas???

Thank you

Perhaps you saw some C++ code that looks like this, which in its simplest form, is very similar in appearance. In C++, this is called forward declaration. C# does not allow forward declarations.

commented: Nice observation. +5

System.Web.Routing.Route in the source code for ASP.Net MVC 2 has these same bodyless constructors. It is not Abstract.

namespace System.Web.Routing
{
	public class Route : RouteBase
	{
		public Route(string url, IRouteHandler routeHandler);

		...

		public IRouteHandler RouteHandler { get; set; }
		public string Url { get; set; }
	}
}

It would appear as though the terse Property get/set syntax is pairing with an additional terse constructor syntax that sets those public properties for you automatically, simply by having constructor parameters with the same name and type (but all lowercase instead of mixed case). Very cool... but I can't find any documentation so far to support this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.