Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract Class Constructor [duplicate]

Tags:

c#

oop

Possible Duplicate:
Why can’t I create an abstract constructor on an abstract C# class?

Can abstract class's Constructor be marked as 'abstract'?

like image 743
NileshChauhan Avatar asked Jul 15 '10 08:07

NileshChauhan


3 Answers

No. C# does not support this in any version. Note that constructors are not inherited in derived classes, although they can be "chained". This is probably what you want to be doing.

If you want to indicate that the derived class should be doing some sort of initialisation, you could create an abstract Initialise method or such which the constructor of the base class (and indirectly of the sub-class) calls on creation.

As a side point, I'm not sure whether the CLR (or associated CIL language) actually supports it - I would suspect it may, though there is little use for it from within C# for the reason just mentioned.

like image 120
Noldorin Avatar answered Oct 04 '22 04:10

Noldorin


No, a constructor cannot be marked as abstract. In abstract classes constructors are usually marked as protected though, at least that's what I would recommend you doing.

like image 22
Darin Dimitrov Avatar answered Oct 04 '22 03:10

Darin Dimitrov


Basically no.

If its abstract, you have to override it in a concrete child class, and you can't override a constructor, only overload it.

like image 24
Russ Clarke Avatar answered Oct 04 '22 04:10

Russ Clarke