Search code examples
c#visual-studionamespacesresharperusing

Type or namespace not found. Visual Studio suddenly demands global prefix on using statements


It happened to me before and I never know why it happens.

Suppose you have a simple C# class with some using statements inside(!) the namespace definition. It worked flawlessly for hours. Now I worked on a completely different project and all of a sudden the former project won't compile anymore claiming a part of the namespace cannot be found anymore. R# then suggests to fix this by using global::...

The type or namespace name 'Internet' does not exist in the namespace 'Cmp.Internet.Portal.Web' (are you missing an assembly reference?)

so

using Cmp.Internet.Portal.Web.Common;

becomes

using global::Cmp.Internet.Portal.Web.Common;

I do understand what it means but I'm puzzled about why it happens. And it should not even be necessary.

An example to make it more obvious...

Let's assume I have 3 projects.

  • Project.Core
  • Project.One (references Project.Core)
  • and Project.Two (references Project.Core)

Now Project.One can have using statements inside its namespace without using the global prefix. Project.Two cannot. Why? ...


Solution

  • I believe the problem occurred because different assemblies provided the same namespaces. It happened because we were in the middle of a refactoring and classes were moved between assemblies.

    Once every namespace was provided by only one assembly the global:: prefix became superfluous.