14

I have a class like this:

namespace Token1.Token2.Token3
{
    public class Class1
    {
    }
}

And another class like this:

namespace Token2.Token4.Token5
{
    public class Class1
    {
    }
}

The first class is a part of my project, the second class is from a framework library developed by another group within my organization. Notice the namespace of the first class has Token2 in the second place and the namespace of the second class has Token2 in the first place.

The problem I am having is that I can't seem to reference the second class within the first because of what looks like a namespace collision. If I try to do this in the first class:

namespace Token1.Token2.Token3
{
    public class Class1
    {
        var frameworkClass1 = new Token2.Token4.Token5.Class1();
    }
}

the Visual Studio IDE highlights Token4 in red and says "Cannot resolve symbol 'Token4'". If I hover my mouse over Token2 where I am new'ing up Class1, intellisense shows me "namespace Token1.Token2" so it is clearly seeing the namespace of my project class and not seeing the namespace of my framework class.

It would be very difficult to change the namespace of either class library due to the amount of code already in place. Is there a way to get around this?

1
  • 2
    The solution to your problem is if it hurts when you do that, don't do that. Do not ever structure namespaces where the top level of one namespace is the same as the second level of another namespace. It causes nothing but pain. Follow standard guidelines to create namespaces that do not have these ambiguities. You say that you cannot change it now; I guarantee you it will be harder to change in the future, so now seems like a good time. Commented Jun 17, 2015 at 0:17

2 Answers 2

28

Since Token2 is also a sub-namespace of Token1 you need to specify that you're looking for the "root" namespace:

var frameworkClass1 = new global::Token2.Token4.Token5.Class1();

You could also alias it with a using statement:

using Token5 = global::Token2.Token4.Token5;

And then just reference the alias:

var frameworkClass1 = new Token5.Class1();
Sign up to request clarification or add additional context in comments.

2 Comments

If you happen to know why it is regarded useful to qualify classes with sub-namespaces, I'd think it would be a good extension to this answer. (Or is there already another question here dealing with it?) Java doesn't have this problem because you always have to use complete paths there, either in the import or in the reference to the class, and I never felt this was a restriction.
I can't answer why (or compare it to Java as I'm not a Java developer) - the good folks that wrote the C# language specs (section 3.8 - Namespace and type names) obviously felt that there was some benefit to searching enclosing namespaces as well.
3

You could try global::Token2 for the framework namespace

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.