Wednesday, June 15, 2011

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();
}
}

Read more: Daily .Net Tips