Avoiding Type Name-Clashes using 'using'

Published 20 October 04 12:58 PM

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

Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Rolando said on October 20, 2004 2:31 PM:
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
# Malcolm Anderson said on October 21, 2004 9:27 AM:
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
# oleg@tkachenko.com (Oleg Tkachenko) said on October 24, 2004 6:51 AM:
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.
# Rob Edwards said on November 15, 2004 12:18 PM:
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.
# RebelGeekz said on December 28, 2004 4:54 AM:
[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/]
# FAQ C# said on April 21, 2005 1:11 PM:
# FAQ C# said on April 21, 2005 1:44 PM:
# FAQ C# (par Yannick Lejeune) said on August 23, 2005 5:26 AM:
# FAQ C# (par Yannick Lejeune) said on August 23, 2005 5:27 AM:
# shiftMode » Blog Archive » Aliases with Using in C# said on April 29, 2006 4:54 PM:
PingBack from http://shiftmode.com/2006/04/aliases-with-using-in-c.html

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker