Namespace Aliases in C#

This article is also available on the Microsoft TechNet Wiki.

The C# language allows developers to create aliases for lengthy fully qualified names or class names. This can come in handy if there is a long namespace or class name that you do not want to have to keep typing out each time. It is also a useful tool to resolve namespace clashes within your code when identical types are imported into your code base.

To accomplish this you can employ the using keyword in C#. The alias lives within the scope of the namespace it Is declared in. When the code compiles, a token is created for the fully qualified name and the alias is then in effect.

For example, the following code snippet will declare an alias:

using diag = System.Diagnostics.Tracing;

The image below displays how to fully declare and use the alias:

2015-09-11_20-50-45

References

About Ken Cenerelli
I am a technical writer/programmer writer/content developer, .NET developer, Microsoft MVP, public speaker, blogger, and Microsoft Azure nerd. I blog about technology at kencenerelli.wordpress.com and am on Twitter via @KenCenerelli.

Leave a comment