Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I create an abstract constructor on an abstract C# class?

I am creating an abstract class. I want each of my derived classes to be forced to implement a specific signature of constructor. As such, I did what I would have done has I wanted to force them to implement a method, I made an abstract one.

public abstract class A {     abstract A(int a, int b); } 

However I get a message saying the abstract modifier is invalid on this item. My goal was to force some code like this.

public class B : A {     public B(int a, int b) : base(a, b)     {         //Some other awesome code.     } } 

This is all C# .NET code. Can anyone help me out?

Update 1

I wanted to add some things. What I ended up with was this.

private A() { }  protected A(int a, int b) {     //Code } 

That does what some folks are saying, default is private, and the class needs to implement a constructor. However that doesn't FORCE a constructor with the signature A(int a, int b).

public abstract class A {     protected abstract A(int a, int b)     {       } } 

Update 2

I should be clear, to work around this I made my default constructor private, and my other constructor protected. I am not really looking for a way to make my code work. I took care of that. I am looking to understand why C# does not let you do this.

like image 289
Anthony D Avatar asked Feb 02 '09 21:02

Anthony D


People also ask

Can we declare an abstract constructor in an abstract class?

We can declare a constructor with no arguments in an abstract class. It will override the default constructor, and any subclass creation will call it first in the construction chain.

Is it possible to create constructor in an abstract class in C#?

Yes, an abstract class can have a constructor, even though an abstract class cannot be instantiated.

Can we create constructor in abstract?

An abstract can have an abstract and a non-abstract method. It must be declared with an abstract keyword. It can have a constructor, static method.

Can an abstract base class have a constructor?

An abstract class can have a constructor similar to normal class implementation.


2 Answers

You cannot have an abstract constructor because abstract means you must override it in any non-abstract child class and you cannot override a constructor.

If you think about it, this makes sense, since you always call the constructor of the child class (with the new operator) and never the base class.

Generally speaking, the only way in C# to enforce a specific constructor signature is by using the new() generic constraint, which enforces the existence of a parameterless constructor for the type parameter.

like image 197
Tamas Czinege Avatar answered Oct 04 '22 08:10

Tamas Czinege


Change that constructor in class A to

protected A(int a, int b) {     // Some initialisation code here } 

Then your subclasses will have to use it, as there is no default constructor.

They can, however, still change the actual signature of the constructor. There is no way of forcing a subclass to use a specific signature for its constructor as far as I know. I'm pretty sure constructors can't be abstract.

What exactly do you need this for? We might be able to suggest a work around for this.

like image 45
Jamie Penney Avatar answered Oct 04 '22 07:10

Jamie Penney