954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Body-less method declaration

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

serban
Newbie Poster
3 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

Either that or the methods were abstract.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 
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();
  }
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 
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.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

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.

DdoubleD
Posting Shark
996 posts since Jul 2009
Reputation Points: 341
Solved Threads: 233
 

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.

soopahman
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You