6

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.

9
  • 2
    because the Nested is public, so the SubOuter class can see it in the base class. Jan 13, 2016 at 8:07
  • 2
    Where is the problem, why you do not want this to compile. Jan 13, 2016 at 8:08
  • 1
    Outer.Nested and SubOuter.Nested is equivalent, there is no difference in them except the way of declaration. Jan 13, 2016 at 8:09
  • 6
    No, you're asking for a new feature. All public members of a base class are accessible through inheriting classes. Why should classes be treated differently then e.g. methods, and why should the compiler team have to specifically add code to prevent you from making such accesses? (And then get the error message translated into various languages, document it, etc) Jan 13, 2016 at 8:14
  • 1
    It's more close to "not inheriting public non-instance members of a class would completely go against what people expect.". It's what I would expect at least. Apart from that, it would probably also have a considerable implementation cost. Jan 13, 2016 at 8:27

2 Answers 2

6

From the perspective of Test Nested is just an identifier as if it were a member e.g. As it is public You may access it everywhere where you cann access any of the classes Outer or SubOuter. However both usages are identical, they identify the same class.

You may even reference the one by the other:

Outer.Nested x = new Outer.Nested(); 
SubOuter.Nested y = x;

Or even

Outer.Nested x = new SubOuter.Nested();
SubOuter.Nested y = x;

However as you may see in the debugger both x and y share a reference to Outer.Nested instead of SubOuter.Nested.

EDIT: As other already mentioned this is no new feature, it is simply the usual treating of members within classes. Thus it surely does not add any benefit to the language as the feature as you describe it simply does not exist. This therefor follows the Principle of least astonishment.

Treating nested classes differenetly however would be a new feature.

3

Because the Nested class is public, so the SubOuter class can see it in the base class.

And you are correct. Outer.Nested and SubOuter.Nested are equivalent, there is no difference in them except the way of declaration.

Update

according to your comment that the features have costs. this feature is already implemented in the .NET (I mean subclass can see the public and protected members of the base class) so there is no additional cost to implement this is .NET.

you can consider the Nested class like any other property in the base class.

this is very important explain which written in the comments on your question by @Damien_The_Unbeliever.

No, you're asking for a new feature. All public members of a base class are accessible through inheriting classes. Why should classes be treated differently than e.g. methods, and why should the compiler team have to specifically add code to prevent you from making such accesses? (And then get the error message translated into various languages, document it, etc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.