C# Frequently Asked Questions

The C# team posts answers to common questions

Avoiding Type Name-Clashes using 'using'

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

Published Wednesday, October 20, 2004 12:58 PM by CSharpFAQ
Filed under:

Comments

 

Rolando said:

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
October 20, 2004 2:31 PM
 

Malcolm Anderson said:

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:\VisualStudioProjects\TestShapes\Class1.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
October 21, 2004 9:27 AM
 

oleg@tkachenko.com (Oleg Tkachenko) said:

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.
October 24, 2004 6:51 AM
 

Rob Edwards said:

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.
November 15, 2004 12:18 PM
 

RebelGeekz said:

December 28, 2004 4:54 AM
 

FAQ C# said:

April 21, 2005 1:11 PM
 

FAQ C# said:

April 21, 2005 1:44 PM
 

FAQ C# (par Yannick Lejeune) said:

August 23, 2005 5:26 AM
 

FAQ C# (par Yannick Lejeune) said:

August 23, 2005 5:27 AM
 

shiftMode » Blog Archive » Aliases with Using in C# said:

April 29, 2006 4:54 PM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker