A C# Riddle

I asked this one during the last blogger dinner, so here it is for everyone else (and those who couldn’t hear me there):

namespace A {
    namespace B {
        public class C {} // #1 - A.B.C

    public class B {
        public class C {} // #2 - A.B+C

    public class D : B.C {}

Without compiling the code above (which actually does compile), what is D’s base class’s full name (both choices are marked) and why?

[Update: Is my face red or what… The code above doesn’t compile, although it’s hard for me to understand why, since I could swear it did a month ago when I found out about this. I’ll try and find what my fault was later today.]

[Update #2: Sorry about that. I’ve posted the correct riddle here.]


10 thoughts on “A C# Riddle

  1. I would say #2 because B (Class) is in the same scope as D (Class).
    I’m probably wrong as is…but I have no idea what it would be if D was
    public class D : A.B.C {}
    (or if it would even compile)

  2. “(which actually does compile)”
    The namespace ‘A’ already contains a definition for ‘B’
    Maybe I am missing something? I wanted to see if I was right. hummmm… maybe you didn’t want me to compile it because it will not complie.

  3. I couldn’t resist, and tried to compile the code. Unfortunately, the code wouldn’t compile.
    Error message can be seen here:
    Did you succeeded compiling it?

  4. Which version of Visual Studio are you using?
    Just tried under VS2005 + SP1 (running on XP) and get:
    The namespace ‘A’ already contains a definition for ‘B’

  5. OK, I cheated. I wanted to confirm my answer, but I get a compile error. I copy and pasted the code exactly and on the class definition for B, it says “The namespace ‘A’ already contains a definition for ‘B’.”
    I would expect the answer to be #2, because I read about the + modifier at some point in relation to nested classes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s