Namespace Alias Qualifier in C#

In this article, I would like to introduce one of the essential feature that makes your code more readable. If you are a C# developer, you have already know about ‘using’ directive and its purpose. In general, using directive allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace. In this article we will go one step further and understand how to make code readable by creating namespace alias qualifier in C#.

Lets take simple example and try to understand the significance of creating namespace alias. In the below example, when you encounter the code line File.Exists() function you may wanted to know the namespace for which the function File.Exist() belongs to. In this case, you need to find the reference via context menu/F12 and check.

Recommended:   C# Threading - Thread Class Constructors

Instead of navigating to the reference via context menu/f12, create the namespace alias. In the below code, I have created the SIO as alias name for System.IO namespace. Hence, my code is more readable as I can easily identity File.Exist() belongs to which namespace on the go (without having navigating to its reference).

Share this:

We will be happy to hear your thoughts

Leave a reply