Is there a way to limit the instantiation of the nested class in C#? I want to prevent nested class being instantiated from any other class except the nesting class, but to allow full access to the nested class from other code.
They have access to both static and non-static members in the enclosing context. They can only define instance members. They're the only type of nested classes that cannot define constructors or extend/implement other classes or interfaces.
A nested class can be declared with any access modifier, namely private, public, protected, internal, protected internal, or private protected.
As a member of its enclosing class, a nested class can be declared private, public, protected, or package private(default).
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.
For the answer proposed by Joshua Smith I found it necessary to force the static constructor of FriendClass to run, achieved by calling an empty static Initalize() method on FriendClass from the static constructor of ParentClass.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With