views:

54

answers:

1

Hi guys,

Look at here (Abstract Class Design): http://msdn.microsoft.com/en-us/library/ms229047.aspx

It says:

(1) Do not define public or protected internal (Protected Friend in Visual Basic) constructors in abstract types.

In C#, we are not able to instantiate an abstract class. So, does it still matter to define public constructors for abstract classes in C# ? Or not writing public constructors for abstract classes is because of the semantic meaning?

It also says:

(2) Do define a protected or an internal constructor in abstract classes.

Define internal constructors ?? In (1), it tells us that not defining internal protected constructors is because that "Constructors with public or protected internal visibility are for types that can be instantiated". Doesn't defining internal constructors for abstract classes break the rules in (1) ?

Thanks in advance. :)

A: 

n C#, we are not able to instantiate an abstract class. So, does it still matter to define public constructors for abstract classes in C# ? Or not writing public constructors for abstract classes is because of the semantic meaning?

Exactly. You don't want the user to see an accessible constructor, but when they call it, they get a compile error.

Define internal constructors ?? In (1), it tells us that not defining internal protected constructors is because that "Constructors with public or protected internal visibility are for types that can be instantiated". Doesn't defining internal constructors for abstract classes break the rules in (1) ?

I believe rule 1 is about public and protected internal rule 2 is about protected and internal. So there's no intersection between the two.

Armen Tsirunyan
Hi Armen. "protected internal" == "protected" + "internal". Now, as documented, "protected" constructor is fine, "internal" constructor is also fine, but why "protected" + "internal" constructor is bad ?
Dylan Lin