#987 – The using Directive Can Create an Alias for a Type

You can use the using directive to create an alias for a namespace, which you can then use to access the types in that namespace.  For example:

using U1 = DogLibrary.Utility.StandardLogging;
using U2 = DogLibrary.Utility.AlternateLogging;

We might do the above if we had an identically named type in each of the two namespaces so that we could then prefix the type with the appropriate namespace.

U1.DogLogger log1 = new U1.DogLogger(@"C:\log1.txt");
U2.DogLogger log2 = new U2.DogLogger(@"C:\log2.txt");

We can also use the using directive to create an alias for a specific type.  For the example above, we could do the following:

using Logger1 = DogLibrary.Utility.StandardLogging.DogLogger;
using Logger2 = DogLibrary.Utility.AlternateLogging.DogLogger;

We could then use the alias directly as a type name:

            // Short for DogLibrary.Utility.StandardLogging.DogLogger
            Logger1 log1 = new Logger1(@"C:\log1.txt");

            // Short for DogLibrary.Utility.AlternateLogging.DogLogger
            Logger2 log2 = new Logger2(@"C:\log2.txt");

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #987 – The using Directive Can Create an Alias for a Type

  1. Pingback: Dew Drop – December 3, 2013 (#1675) | Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: