Zverev Evgeniy
Zverev Evgeniy

Reputation: 3712

Visual Studio 2015 intellisense and ambiguous namespaces

The Xamarin.Auth project broke up i.e. the source code was compiling OK and after another Xamarin.Android update it was suddenly broken.

It turned out that the problem arose after the Mono.Android.dll (Xamarin.Android) was extended by several namespace definitions.

I hunted the problem down to the following:

There is a dll #1 (Mono.Android.dll):

namespace Xamarin.Android
{
    public class Class1
    {
    }
}

namespace Android.OS
{
    public class Class2
    {
    }
}

There is a dll #2 (Xamarin.Auth.Android.dll):

namespace Xamarin.Auth
{
    //This does not compile. See the problem description below.
    public class User1 : Android.OS.Class2
    {
    }
}

Intellisense shows the following problem:

Error CS0234 The type or namespace name 'OS' does not exist in the namespace 'Xamarin.Android' (are you missing an assembly reference?)

This can be fixed by changing the latter namespace to something else or by using the global:: identifier:

namespace SomeOtherNamespace
{
    //This compiles ok.
    public class User1 : Android.OS.Class2
    {
    }
}

namespace Xamarin.Auth
{
    //This compiles ok.
    public class User1 : global::Android.OS.Class2
    {
    }
}

The question is: why does not intellisense give a warning that the Android namespace branch is ambiguous between global::Xamarin.Android and global::Android? What is the good way out of it? Always use global:: namespace identifier?

Upvotes: 1

Views: 374

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156928

You can use the global directive to tell the compiler it should evaluate the namespace from the root, else it tries to evaluate the namespace relative from the current namespace. Using global always uses the full qualified namespace name, so that is why it works when you prefix it.

Another option would be using an alias:

using AOS = Android.OS;

namespace Xamarin
{
    public class User1 : AOS.Class2
    {
    }
}

Upvotes: 1

Related Questions