944,123 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 2265
  • C# RSS
Oct 23rd, 2009
0

Body-less method declaration

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
serban is offline Offline
3 posts
since Oct 2009
Oct 23rd, 2009
0
Re: Body-less method declaration
That doesn't compile in 3.5 and I think the error message answers your question:
text Syntax (Toggle Plain Text)
  1. 'daniweb.MyClass.MyClass()' must declare a body because it is not marked abstract, extern, or partial
  2. '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:
C# Syntax (Toggle Plain Text)
  1. public interface ITest
  2. {
  3. void ITest();
  4. }

I suspect you were looking at partial class definitions.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 23rd, 2009
0
Re: Body-less method declaration
Either that or the methods were abstract.
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Oct 23rd, 2009
0
Re: Body-less method declaration
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:
C# Syntax (Toggle Plain Text)
  1. public abstract class AbstractClass
  2. {
  3. abstract AbstractClass();
  4. }
  5. public abstract class AbstractClass2
  6. {
  7. AbstractClass2();
  8. }
  9. public class AbstractClass3
  10. {
  11. abstract AbstractClass3();
  12. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 23rd, 2009
2
Re: Body-less method declaration
Click to Expand / Collapse  Quote originally posted by sknake ...
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.
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Oct 24th, 2009
1

Note to self: why is this title field here?

Click to Expand / Collapse  Quote originally posted by serban ...
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.
Last edited by DdoubleD; Oct 24th, 2009 at 9:33 am.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 3rd, 2010
0

System.Web.Routing.Route - bodyless constructors

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

C# Syntax (Toggle Plain Text)
  1. namespace System.Web.Routing
  2. {
  3. public class Route : RouteBase
  4. {
  5. public Route(string url, IRouteHandler routeHandler);
  6.  
  7. ...
  8.  
  9. public IRouteHandler RouteHandler { get; set; }
  10. public string Url { get; set; }
  11. }
  12. }

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
soopahman is offline Offline
1 posts
since Sep 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Ternary operator order problem.
Next Thread in C# Forum Timeline: Regarding Windows Application DateTimePicker





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC