Search code examples
c#assembliesnamespaces

Having problems when two of referenced assemblies both define type A.A1


If two assemblies both define namespace A containing class A1, then the two classes are considered unique types.

a) Are the two namespaces also considered unique?

b) If program P has a reference to both assemblies, how do we create an instances of the two types? Namely, I keep getting an error when I try to create an instance of A.A1

using A;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            A1 a = new A1(); // error
        }
  }
}

c) But if program P also defines type B.A1, then compiler doesn’t complain when I declare an instance of A1:

using A;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            A1 a = new A1(); // ok
        }
    }

    class A1 { }
}

Shouldn’t compiler complain, since it can’t know which version of A1 to use ( A.A1 from one of the referenced assemblies or B.A1 )?

thanx


Solution

  • You can resolve this with the extern alias directive.

    And here is a better explanation.