Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visibility of nested class constructor

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.

like image 720
Bogi Avatar asked Apr 29 '10 11:04

Bogi


People also ask

Can nested class have constructor?

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.

Can we have public protected access modifiers in nested class?

A nested class can be declared with any access modifier, namely private, public, protected, internal, protected internal, or private protected.

Can nested classes be private?

As a member of its enclosing class, a nested class can be declared private, public, protected, or package private(default).

Can nested classes access private members in Java?

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.


1 Answers

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.

like image 139
Martin Fay Avatar answered Oct 13 '22 05:10

Martin Fay