Global Namespace Alias, Object alias and External Alias

I wanted to show three ways how to set aliases for namespaces, objects and assemblies. Less known are the Assembly External aliases that are supported in Visual Studio.

Check the following snipped, which uses all possible aliases.

Code Snippet

    extern alias externsys;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using sys = System;
    using con = System.Console;
    namespace AliasTests
    {
    class Program
        {
    static void Main(string[] args)
            {
    // Full namespace
                System.Console.WriteLine("Hello");
    // Global Namespace alias
                sys::Console.WriteLine("Hello");
    // External alias
                externsys::myLib.Writer.WriteText("Hello");
    // Object alias
    con.ReadLine();
            }
        }
    }

Namespace alias is just renaming of an existing namespace to different one.

Type alias is shortcut for a type from its full name (with namespace)

External alias is an identifier of specific assembly, where is the namespace taken from. By default, there is an external alias global for all referenced assemblies.

To set external alias in Visual Studio, you need to go to Solution Navigator, select an assembly.

Links

Extern alias walkthrough – http://blogs.msdn.com/b/ansonh/archive/2006/09/27/774692.aspx

How to: Use the Namespace Alias Qualifier (C# Programming Guide) – http://msdn.microsoft.com/en-us/library/c3ay4x3d(v=vs.80).aspx

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 )

Google+ photo

You are commenting using your Google+ 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