Tuesday, January 01, 2008 12:35 PM
TanzimSaqib
Namespace Alias Qualifier - to get rid of crazy coding
Let us say somebody in your company loves crazy coding and really do not bother about his/her codes affect others. (S)He put class name System and a constant Console and now wondering how come a simple Console.WriteLine does not compile:
class System
{
int Console = 10;
static void Main(string[] args)
{
Console.WriteLine("Hello World!"); // Compile time error
System.Console.WriteLine("Hello World!"); // Compile time error
}
}
Making use of global::System.Console must solve your problem:
class System
{
int Console = 10;
static void Main(string[] args)
{
global::System.Console.WriteLine("Hello World!");
global::System.Console.WriteLine("Hello World!");
}
}
However, ever thought of a scenario where there can be same class name under two different namespaces? Here comes the role of Namespace Alias Qualifier. In namespace declaration by "using", aliases can be assigned to namespaces so that they might be useful in later part of the code as shorthand and most importantly will solve the problem of ambiguity:
using sys = System;
using mine = MyProject.Process;
...
...
sys.Console.WriteLine(mine.Console["width"]);
Filed Under:
C#
Anonymous comments are disabled
About TanzimSaqib
Tanzim Saqib is a
Senior Developer, who spent half of his life on software and worked for many companies like #1 .NET controls provider
Telerik Inc, #1 personalized Web 2.0 start-page like
Pageflakes (acquired by LiveUniverse). He developed many projects ranging from banking solutions for Citibank, HSBC, Wamu, Wells Fargo etc. to
Paperless Virtual University. He is industry's earliest and leading widget developer and as know as
"Widget Master" to his peers.
He is a preacher of Microsoft technologies. While he jams with the latest additions to .NET, in his spare time he blogs at
http://weblogs.asp.net/TanzimSaqib, maintains his personal website
http://www.TanzimSaqib.com, leads
.NET Research group. writes articles.
He is an
easy going, fun loving, and
passionate technology individual who is open to any kind of
business opportunity and professional relationship. He currently lives in Bangladesh, but travels anywhere in the world on professional demand.
Email: me at TanzimSaqib dot com