1

From MSDN: Abstract Class Design:

X DO NOT define public or protected internal constructors in abstract types.

√ DO define a protected or an internal constructor in abstract classes.

Is there contradiction here? Or does it mean that constructor should be or protected or internal but not protected and internal at the same time?

Community
  • 1
  • 1
Alexan
  • 8,165
  • 14
  • 74
  • 101

1 Answers1

2

Yes, it does mean that a constructor should not be protected and internal at the same time, but either protected or internal is ok. Protected (and) internal is as misleading as public because constructors of abstract classes can only be called from inheritors.
A single protected or internal make a difference as they allow a different range of inheritors (internal allows only inheritors from the current assembly, protected allows any inheritor). But public extends the accessibility over the range an abstract class can provide by its nature. Protected internal does nothing more than a single protected for an abstract class.
That's why they are misleading.

Markus
  • 20,838
  • 4
  • 31
  • 55
  • Still don't understand why it can't be internal and protected at the same time. Internal is accessible inside the same assembly, protected is from inheritor. If you want to access for inheritor inside assembly it should be internal and protected. – Alexan Nov 02 '13 at 17:27
  • okay, from here: http://stackoverflow.com/questions/585859/what-is-the-difference-between-protected-and-protected-internal: it is the union of the two, not the intersection, so protected internal is accessible by any class in the same assembly and also any inheritor from other assemblies. But still why not? – Alexan Nov 02 '13 at 17:36
  • 1
    It can, but it is a design advice not to do it, because it does not add anything useful. For a non-abstract class, an internal constructor can be used to construct the object from within the assembly. In this case protected internal does make a difference, because it allows both construction in the assembly and access from inherited classes outside the assembly. For an abstract class, instatiation is not relevant, so the combination does not differ from a single protected. – Markus Nov 02 '13 at 17:42