protected vs public constructor for abstract class? Is there a difference?

Marlon picture Marlon · Dec 26, 2010 · Viewed 11.1k times · Source

This question is out of curiosity. Is there a difference between:

public abstract class MyClass
{
    public MyClass()
    {
    }
}

and

public abstract class MyClass
{
    protected MyClass()
    {
    }
}

Thanks.

Answer

Mark Byers picture Mark Byers · Dec 26, 2010

They are the same for all practical purposes.

But since you asked for differences, one difference I can think of is if you are searching for the class's constructor using reflection, then the BindingFlags that match will be different.

BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
var constructor = typeof(MyClass).GetConstructor(flags, null, new Type[0], null);

This will find the constructor in one case, but not the other.