A C# Riddle, Try #2

The code below compiles (sorry about that again) and is actually the code problem I originally wanted to post. Now play the compiler – which is Bar’s base class and why:

namespace NS
{
namespace NS
{
class Cls // Candidate #1
{
}
}
class Cls // Candidate #2
{
}
namespace Foo
{
class Bar : NS.Cls // Who am I?
{
}
}
}

For extra credit, how do I force the compiler to choose the other one?

[Update: Good going, everyone! Candidate #2 is indeed the one used. To use candidate #1, you must either apply the ‘global::’ prefix or explicitly use the class’s full name.]

Advertisements

6 thoughts on “A C# Riddle, Try #2

  1. Candidate #1
    Choose candidate #1 by inheriting just Cls
    Just a guess, I haven’t tested it.

  2. Saifee is correct
    The :: operator is the namespace alias qualifier operator where global:: would start at the global namespace
    You could do
    using doubleNs = NS.NS;
    class Bar : doubleNs::Cls // Who am I?
    {
    }
    to alias the NS.NS namepace

  3. It Will take candidate 1, since the class is already under the namespace NS and when u write ns.cls u are reffering to the the class inside the name space ns in side the current name space.
    To get the other class use the base class as cls.
    Vikram

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s