2

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'?

3
  • 2
    This means that class can't be inherited by application developers. It actually has an internal ctor, so it can be inherited by the library authors. Commented Nov 21, 2023 at 17:33
  • 1
    It's likely that they have an internal constructor, meaning that the two derived types can inherit from them because they are defined in the same assembly so have the appropriate access. However the documentation is correct that to external code/readers of the documentation, that you can't inherit from it. Commented Nov 21, 2023 at 17:34
  • 1
    internal constructor and associated comment: github.com/dotnet/runtime/blob/… Commented Nov 21, 2023 at 17:36

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

2

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.