Consider the following code example:
class Outer
{
public class Nested { }
}
class SubOuter : Outer { }
class Test
{
Test()
{
Outer.Nested x; // Makes sense.
SubOuter.Nested y; // Compiles, but why?
}
}
It appears that the nested class is "inherited" by the subclass. Where's the point in that? What can I do with this feature that I cannot (or cannot easily) do otherwise? Aren't Outer.Nested
and SubOuter.Nested
exactly equivalent?
Clarification: Of course it compiles because the C# spec says so. I understand that. I am asking why C# was designed that way, since it does not seem to add something to the language. If you have an example to the contrary, i.e., some code that gets easier/shorter/better by using this feature, please share it in an answer and I will gladly accept it.
Nested
is public, so theSubOuter
class can see it in the base class. – ClyveOuter.Nested
andSubOuter.Nested
is equivalent, there is no difference in them except the way of declaration. – ClyveOuter
andSubOuter
are class names, not object instances. – Casseycassi