Extern Aliases in .Net

Whenever want to refer two types with the same full qualified name (namespace & type name is same), we have to use extern aliases. That means if there two assemblies contain types with same name and namespace, we have to refer those types with extern aliases.

For example, there are two assemblies Assembly1.dll and Assembly2.dll which contains Company.Employee class as below. 

Assembly1.dll 

namespace Company
{
        public class Employee
        {  

        }
}

Assembly2.dll

 

namespace Company
{
        public class Employee
        {  

        }
}

Open Microsoft Visual Studio and add references to these two assemblies. Right click on Assembly1 reference and select properties to change alias name to Assembly1.dll as shown below.

 

Do the same for Assembly2 reference and change alias name to Assembly2. To call these two assemblies types, we have to provide extern aliases for these two types as shown below.

extern alias Assembly1;
extern alias Assembly2;  

namespace CSharpExternAlias
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly1.Company.Employee employee1 = new Assembly1.Company.Employee();

            Assembly2.Company.Employee employee2 = new Assembly2.Company.Employee();
        }
    }
}

As shown we can call two assemblies classes through extern alias even though two classes has a same full qualified name.