Why can't I create an abstract constructor on an abstract C# class?
Posted
by Anthony D
on Stack Overflow
See other posts from Stack Overflow
or by Anthony D
Published on 2009-02-02T21:21:00Z
Indexed on
2010/05/05
19:58 UTC
Read the original article
Hit count: 195
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.
© Stack Overflow or respective owner