Mohsen Mohsen - 8 days ago 6
C# Question

Why C# doesn't throw error on ambiguous namespaces?

Assume we have two projects with different namespaces.

using Models = SharedClasses;

namespace MainAspNetMvcApplication{
private void Method1(){
var c = new Models.Cat();
//Error CS0234 The type or namespace name 'Cat' does not exist in the namespace 'MainAspNetMvcApplication.Models' (are you missing an assembly reference?)
}
}

namespace MainAspNetMvcApplication.Models{
//There is no class `Cat` in this namespace
public class Dog {
...
}
...

}


I have added reference to a
SharedClasses
which contains following code.

namespace SharedClasses{
public class Cat{
//The code for shared classe (Cat)
}
}


This is more like to an ambiguous
Models
error. However C# assumes current project
Models
namespace and throws
CS0234
error.

Why by default c# assume current project
Models
namespace while I have mentioned to use
Models
for
SharedClasses
?

For me, the highest priority should be considered for
using Models = SharedClasses;


Shouldn't at least C# let me know I have two ambiguous
Models
instead of choosing one ?

Just curious why C# designed like this. While I'm looking for language designers to answer any other related answer is welcome.

Answer

No alias is something different.

using Models = SharedClasses;

Now Models is an alias not a namespace, so it is not ambigious namespace there are multiple rules outlining behavior of aliases you can find them here:

Using alias directive

Comments