The closest you can get to this type of encapsulation is using a partial parent class:
internal partial class Parent
{
private Child1 c1Instance = new Child1();
private Child2 c2Instance = new Child2();
}
internal partial class Parent
{
private class Child1
{
}
}
internal partial class Parent
{
private class Child2
{
}
}
You can split these up into multiple files, the end result will be the same - Child1
and Child2
will be private classes internal to Parent
and inaccessible elsewhere.
Makes it a bit more confusing sometimes, but I think it's the closest thing to what you are trying to achieve.
Clarification
A C# source file can hold any number of namespaces, and each namespace can contain any number of structs and classes.
This is a valid (but not very functional) C# source file:
using System;
namespace FirstNS
{
public class Class1
{
}
public class Class2
{
}
public partial class Parent1
{
}
public partial class Parent1
{
private class Child1
{
}
private class Child2
{
}
}
}
namespace FirstNS.ChildNS
{
public class Class3
{
}
}
Compiling that gives you the following classes:
FirstNS.Class1
FirstNS.Class2
FirstNS.Parent1
FirstNS.Parent1.Child1 (private)
FirstNS.Parent1.Child2 (private)
FirstNS.ChildNS.Class3
You could also split each of the class definitions above into multiple files, if you have a reason to do so.