Namespace Aliasing

Problem
You want to know how to apply namespace aliasing.

Solution
When using types with the same name from different namespaces, alias all of them.

Example
The System.Windows.Forms.Control and the System.Web.UI.Control types are both contained in the .NET Framework. Let’s say, for example, you’re writing a class library that needs both types. You need to import both namespaces, and it is recommended that you alias both.

// Not great since only one namespace is aliased.
using System.Windows.Forms;
using Web = System.Web.UI;
...
// Referencing the two different Control classes.
Control controlOne; // The System.Windows.Forms.Control.
Web.Control controlTwo; // The System.Web.UI.Control.

// This is better since both namespaces are aliased.
using Win = System.Windows.Forms;
using Web = System.Web.UI;
...
// Referencing the two different Control classes.
Win.Control controlOne; // The System.Windows.Forms.Control.
Web.Control controlTwo; // The System.Web.UI.Control.

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 )

w

Connecting to %s