tags:

views:

269

answers:

5

Why is the following code snippet valid in C#? This is a feature of the compiler or a bug?

class A
{
    public class B : A
    {

    }
}

class C : A.B
{
    public void Foo(C.B b)
    {

    }
}

class D : A
{
    public void Foo(D.B.B.B.B b)
    {

    }
}

See Also

.NET Nested Classes

+6  A: 

It's legal code, but quirky. I can dig out the bit in the spec about name resolution if you want - but it's definitely legal. I've had a conversation about a similar topic with Eric Lippert before. Strangely enough, that used D.B.B.B.B... too.

The conversation came up due to this earlier question.

The relevant section of the C# 3.0 spec is 3.8, but it's too long and involved to be worth posting here.

Jon Skeet
A: 

See link as it is very similar to a question I had a few months ago...

http://stackoverflow.com/questions/455928/-net-nested-classes

JTA
+1  A: 

This is rather amusing. I don't know how it could be any harm, though.

mquander
+1  A: 

Note that the code analysis guidelines state that nested types should not be visible.

Dan
+3  A: 
  1. D is an A
  2. A has a nested type B
  3. B is an A
  4. GOTO 2
BCS