views:

30

answers:

2

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

+1  A: 

Referencing two assemblies having the same namespaces and same members within those namespaces is a complete no-no (i.e. don't do it!). I you have control over one or other of the assemblies, ensure that the root namespaces are different for the two and then you can disambiguate references to members within the assembly/namespace hierarchy.

Will A
+1  A: 

You can resolve this with the extern alias directive.

And here is a better explanation.

Henk Holterman
thank you both for your help
flockofcode