Namespace and Class Alias in C#

We use using to define alias. There are 2 types of Alias

 1. Namespace Alias: Namespace alias are used when you want to shorten a long namespace. To do that :

 using Abhishek = System.Drawing; 
public class X 
{ 
public X()
    { 
       Abhishek.Graphics g = this.CreateGraphics();
     } 
}
 

Here in the header we made an alias Abhishek of System.Drawing

Thus within the code if we refer to Abhishek, it will be same as referring to System.Drawing.

2. Class Alias: You can also make use of using statement to define reference to a single class. Say if I write using Abhishek=System.Drawing.Bitmap; Abhisek will hold the class Bitmap. We can create Object of Abhishek, access static functions directly.

 using Abhishek=System.Drawing.Graphics;
 public class X 
{ 
    public X() 
        { 
            Abhishek g = this.CreateGraphics();
         } 
     } 

Thus Abhishek will be pointing to native Graphics object rather than the entire namespace.

For such many stuff please read Uncommon C# keywords

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434