Namespace collisions


Answers

Is your code in namespace A.B or A.B.C? If so, that's probably the issue. Use a using directive like this:

using TheTypeIWant = A.B.C.D.MyType;

then just refer to TheTypeIWant in your code.

EDIT: I've just tried the "using MyType=A.B.C.D.MyType" option, but that doesn't work. The above is fine though.

Question

How is it possible that .NET is finding the wrong 'MyType' in this scenario?

I have a type A.B.C.D.MyType in a project that I'm working on, and I'm referencing a DLL that has a type A.B.MyType? I do not have any 'using A.B;' statements anywhere in my code, and I do have 'using A.B.C.D;'. When I compile, the compiler thinks any naked reference to 'MyType' means 'A.B.MyType'.

I know I could just rename the class or use an alias, but I'm wondering how this is even possible.

Any ideas?

Thanks!




Alias the namespace in the using statement:

using ThatOuterFoo = Foo.Uber;
...
...
//Some time later...
var x = ThatOuterFoo.Bar();



All System namespaces are undefined in project

You could either try to use an alias when you add your using declarations:

using CustomSystem = SolutionNamespace.WhatItIs.System;

And then refer to those members like this:

CustomSystem.SomeClass = new CustomSystem.SomeClass();

Or just not add it to the using declarations and fully qualify each use:

SolutionNamespace.WhatItIs.System.SomeClass = 
    new SolutionNamespace.WhatItIs.System.SomeClass();

UPDATE >>>

If that doesn't help, you could take a look at the Namespace collisions and C# Namespace Alias qualifier (::) vs Dereferencing Operator (.) posts here on , which have good answers. I think that @Zache could be correct in mentioning that you might need to use the Global Namespace Alias.




Yes, it does - to a small extent. There is an edge-case to do with scoping / local names, see Eric Lippert's blog.

For a concrete example (specific to alias usage):

using System;
using Foo = Bar;
public static class Bar {
    public  static void Test() { Console.WriteLine("outer"); }
}
namespace MyNamespace {
    //using Foo = Bar;

    public static class Bar {
        public static void Test() { Console.WriteLine("inner"); }
    }
    static class Program {
        static void Main() {
            Foo.Test();
        }
    }
}



Absolute/outer and inner namespace confusion in C#

You can do this using a namespace alias qualifier (typically global::) to refer to the default / root namespace:

global::Foo.Uber







Tags