.NET documentation confusion around inheritance

81 views Asked by At

I am reading the .NET documentation, and encountered the IdentityReference class, which states:

Represents an identity and is the base class for the NTAccount and SecurityIdentifier classes. This class does not provide a public constructor, and therefore cannot be inherited.

My confusion is that it says it cannot be inherited, yet it also states it is the base class for the NTAccount and SecurityIdentifier classes. Based on my understanding, these classes are inheriting IdentityReference.

Is the documentation incorrect, or is my understanding incomplete? Should the documentation say 'This class does not provide a public constructor, and therefore cannot be instantiated'?

2

There are 2 answers

0
Panagiotis Kanavos On BEST ANSWER

The documentation is correct. A child class must be able to call a base class constructor, whether the default parameterless constructor or another one. The IdentityReference class has no public or protected constructor though. If you try to inherit from it, you'll get a compilation error.

The class has an internal constructor, which allows classes in its own assembly to inherit from it

public abstract class IdentityReference
{
    internal IdentityReference()
    {
        // exists to prevent creation user-derived classes (for now)
    }

NTAccount is defined in the same assembly so it can inherit from IdentityReference:

public sealed class NTAccount : IdentityReference
{
...

The class is sealed though so other assemblies can't inherit from it either

0
Louis Ingenthron On

Most likely, what this means is that the constructor is internal, which means the class can be inherited only by classes in the same assembly. So, when it says it "cannot be inherited" it really means it "cannot be inherited by you, the author of an external assembly".