2

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.

Mohsen Sarkar
  • 5,910
  • 7
  • 47
  • 86
  • Did you include your namespace or put an assembly reference somewhere? – devRicher Nov 26 '16 at 10:54
  • @devRicher I have added the reference, and it's also included with `using Models = SharedClasses;` – Mohsen Sarkar Nov 26 '16 at 10:56
  • What if you dont use Models = SharedClasses? – tymtam Nov 26 '16 at 11:01
  • Please, clarify your question. You want specification reference (AFAIK, behavior, you see, conform specification)? Or you want to language designers to answer, why them design C# like this? – user4003407 Nov 26 '16 at 11:02
  • @mayu Nothing changes. The same error on the same line. – Mohsen Sarkar Nov 26 '16 at 11:05
  • Hmm... Are you sure that the error message is the same. If you dont use the word Models why would the compiler complain about Cat not being in Models? – tymtam Nov 26 '16 at 11:07
  • To be clear - I suggest removing the 'using' line and changing 'new Models.Cat' to 'new SharedClasses.Cat'. – tymtam Nov 26 '16 at 11:09
  • Your `using` directive is _outside_ the `namespace` declaration. Therefore, when there is an ambiguity (`Models` can refer to several things), the type or namespace existing in an "inner" scope is preferred over the `using` directive which is in an "outside" scope. See [my answer elsewhere](http://stackoverflow.com/a/16092975/1336654) for details. For that reason, no ambiguity compile-time error is issued in this case for `Models`; there are rules determining which type or namespace the compiler must pick. – Jeppe Stig Nielsen Nov 26 '16 at 12:45

1 Answers1

3

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

Mateusz
  • 2,287
  • 20
  • 28