I\'m looking to define a nested class that is accessible to the container class and external classes, but I want to control instantiation of the nested class, such that only
It is impossible to declare class in such way. I think that the best way for you would be to declare class as private and expose it through public interface:
class Program
{
static void Main(string[] args)
{
// new S.N(); does not work
var n = new S().Create();
}
}
class S
{
public interface IN
{
int MyProperty { get; set; }
}
class N : IN
{
public int MyProperty { get; set; }
public N()
{
}
}
public IN Create()
{
return new N();
}
}