#529 – The using Directive Can Create an Alias for a Namespace
February 29, 2012 Leave a comment
The most common use of the using directive is to bring types within a specified namespace into scope, so that they can be referenced directly in your code.
using DogLibrary; using DogLibrary.Utility;
You can also use the using directive to create an alias for a namespace, which you can then use to access the types in that namespace.
In the example below, we have two different namespaces that each contain a DogLogger type. Instead of having to list the fully qualified name of either DogLogger type when we use it, we can assign a shorter alias to refer to the containing namespace.
using U1 = DogLibrary.Utility.StandardLogging; using U2 = DogLibrary.Utility.AlternateLogging; namespace ConsoleApplication1 { class Program { static void Main() { // Short for DogLibrary.Utility.StandardLogging.DogLogger U1.DogLogger log1 = new U1.DogLogger(@"C:\log1.txt"); // Short for DogLibrary.Utility.AlternateLogging.DogLogger U2.DogLogger log2 = new U2.DogLogger(@"C:\log2.txt"); } } }