Hi Jake,
Sometimes nested classes are made public.
A nested, public class can be useful when two classes are closely related,
when one class has purpose only when its used along with the other class,
and when one needs to access private implementation details of the other,
but must remain publicly accessible.
For instance, the System.Windows.Forms.Control.ControlCollection class.
It's a nested, public class that can be inherited as well.
When an instance of the nested class has a reference to an instance of the
containing class, it can access its private members. If it were to be
declared externally than those private members would have to be marked
internal, making them visible to all classes within the same assembly.
Here's an example of two classes. The Outer class contains the nested,
Inner class and the Inner class references the Outer class to access its
private members:
public class Outer
{
static void Main()
{
Outer outer = new Outer("This is the outer value.");
Outer.Inner inner = new Outer.Inner(outer);
inner.WriteOuterValue();
}
string value;
public Outer(string initialValue)
{
value = initialValue;
}
public class Inner
{
Outer outer;
public Inner(Outer outer)
{
this.outer = outer;
}
public void WriteOuterValue()
{
Console.WriteLine(outer.value);
}
}
}