Dot Net For All

Aliases for Type and resolving external assembly collision

Type Aliases

There can be chances that we have two classes of same name residing in the same assembly or the different assemblies but we cannot  change the name of the classes due to design restrictions.

Suppose I have a class in AssemblyA as shown in the below figure 1.

And I am referencing the AssemblyA in my project and using instance of MyClass as shown in the following code snippet.

using AssemblyA;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TypeAliases
{
    class Program
    {
        static void Main(string[] args)
        {
            var instance = new MyClass();
        }
    }
}

Now a need arises in the project to create a new class named MyClass in the same project but could be in the same assembly or in different assembly. And we have to name the new class as MyClass only due to some naming restrictions or due to some other concerns as shown in the following figure.

Now if we have to access the new class along the class created previously we have to use a fully qualified name of the class along with the assembly as shown in the code snippet below.

static void Main(string[] args)
        {
            var instance = new MyClass();
            var instance2 = new AssemblyA.SubAssembly.MyClass();
        }

The above code can be bit clumsy and difficult to maintain if we have to use the same class at many places.
Instead we can create a type aliases as shown in the following code snippet and use it instead of full namespace name.

using AssemblyA;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyClass2 = AssemblyA.SubAssembly.MyClass;

namespace TypeAliases
{
    class Program
    {
        static void Main(string[] args)
        {
            var instance = new MyClass();
            var instance2 = new MyClass2();
        }
    }
}

When we execute the above code we can see that the instance2 is created of the MyClass present in the sub folder of AssemblyA as shown in the figure.

Resolving Assembly Conflict for Classes

Sometimes there are cases that a same class name exists in two assemblies with same namespaces which we want to refer to in our project. If both of these assemblies are created by us in that case we can simple change the name of the class as well as the assembly if possible. But suppose both of these assemblies are developed by third party in that case it is almost impossible to change the name of the class. In that case the compiler will not be able to figure out which namespace to refer to create instance of the class.
Suppose I have two assemblies ClassLibrary1 and ClassLibrary2 but with same namespace and class name as MyNamespace and MyClass respectively as shown in the below code snippets.

namespace MyNamespace
{
    public class MyClass
    {
        public override string ToString()
        {
            return "I am in ClassLibrary1";
        }
    }
}
namespace MyNamespace
{
    public class MyClass
    {
        public override string ToString()
        {
            return "I am in classLibrary2";
        }
    }
}

If I add reference to both of these assemblies and if I try to create an instance of MyClass we will get a compile time error as “MyClass exists in both ClassLibrary1 and ClassLibrary2 ” or “fully qualify type exists in both assembly”
To resolve this error we have to give the alias to both of these assemblies as shown below.
Right click on the assembly and open the properties as shown in the below figure. I have changes the alias for both the assemblies to Lib1 and Lib2.

After changing the alias for the namespace we have to refer to the changed alias in our code as shown below.

extern alias Lib2;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace TypeAliases
{
    class Program
    {
        static void Main(string[] args)
        {
            var inst1 = new Lib1.MyNamespace.MyClass();
            var inst2 = new Lib2.MyNamespace.MyClass();
        }
    }
}

After debugging the above code we can see that both of these instances created are of the respective classes.

In this blog I have demoed how we can we can add type alias as well as assembly alias.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview