Is it possible to have a nested class that is public but can only be instantiated by it's parent class e.g.
public class parent
{
public class child
{
public string someValue;
}
public child getChild()
{
return new child();
}
}
in this example the 'child' class could be instantiated by code external to 'parent' . I want the external code to be able to view the 'child' type but not be able to construct it's own one.
e.g
var someChild = new parent.child();
getChild
method make a new child and return the interface type.As mentioned in the comments and other answers, you can also change the access modifier on the constructor(s) of the inner class to either internal or private while leaving the inner class itself public.
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