Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
You are already aware that the C# using keyword allows you to supply hints to the compiler regarding the fully qualified name of the types within a given *.cs file. However, what you may not know is that the using keyword also allows you to build aliases (very helpful for prevent name clashes). Assume you have the following two namespace definitions:
namespace My2DShapes
{
public class Hexagon{}
}
namespace My3DShapes
{
public class Hexagon{}
}
Now assume you wish to create an instance of the 3D Hexagon from the following application:
using My2DShapes;
using My3DShapes;
public class MyApp
{
public static void Main()
{
// Error! Which Hexagon?
Hexagon h = new Hexagon();
}
}
This name clash can be resolved quite simply by building the following alias:
using My2DShapes;
using The3DHex = My3DShapes.Hexagon;
public class MyApp
{
public static void Main()
{
// This really creates a new My3DShapes.Hexagon.
The3DHex h = new The3DHex();
}
}
Tip from Andrew Troelsen
Posted by: Duncan Mackenzie, MSDN
This post applies to Visual C# .NET 2002/2003/2005
Comments
- Anonymous
October 20, 2004
I have also used "Using" to solve Namespace conflicts with namespaces using System in their names.
http://dotnetjunkies.com/WebLog/dotnetrolando/archive/2004/09/17/25936.aspx - Anonymous
October 21, 2004
I'm missing something here.
This is legal
using The3DHex = My3DShapes.Hexagon;
but this isn't ?
using My2DShapes.Hexagon;
why does The3DHex work, but the My2DShapes yields
C:VisualStudioProjectsTestShapesClass1.cs(1): A using namespace directive can only be applied to namespaces; 'My2DShapes.Hexagon' is a class not a namespace
Isn't The3DHex refering to a class?
Thanks
Malcolm - Anonymous
October 23, 2004
Malcolm, take a look at the C# spec:
using [alias = ]class_or_namespace;
where:
alias (optional)
A user-defined symbol that you want to represent a namespace. You will then be able to use alias to represent the namespace name.
class_or_namespace
The namespace name that you want to either use or alias, or the class name that you want to alias.
Last sentense means class name can only be used for defining an alias. - Anonymous
November 15, 2004
The difference is that the using command by itself is a namespace directive, ie: using My2DShapes.Hexagon;
When you include the '=' then you are in fact creating an alias with the using command. So the compiler is correct in telling you that you are trying to 'use' a class and not a namespace. - Anonymous
December 27, 2004
[http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/] - Anonymous
April 29, 2006
PingBack from http://shiftmode.com/2006/04/aliases-with-using-in-c.html