Using Namespace Aliases

11 October 2007 - 1:43 PM / by Dominic Pettifer. 1 Comment

Cool C# Snippets - Ever get tired of prefixing class names with the fully qualified namespace to avoid class name and namespace conflicts (eg. System.Namespace.AnotherNamespace.User). Well there’s a way around it using Namespace Aliases.

Type Less with Namespace Aliases

Ever grow tired of writing this...

System.Web.Namespace.AnotherNamespace.YetAnotherNamespace.User user =
	new System.Web.Namespace.AnotherNamespace.YetAnotherNamespace.User();

...all so you can use the User object in that namespace without it conflicting with about a gazillion other User classes elsewhere? If so then use a Namespace alias eg...

using MyAlias = System.Web.Namespace.AnotherNamespace.YetAnotherNamespace;

and in your code...

MyAlias.User user = new MyAlias.User();

Alternatively you can also alias the class itself eg...

using CustomUser = System.Web.Namespace.AnotherNamespace.YetAnotherNamespace.User;

and in code...

CustomUser user = new CustomUser();

Pretty cool time saving technique I reckon.

And whoa, that’s my first post on this blog in nearly 10 months! Seems I hardly get any time to write these things, so I'm going to try and write them shorter like this.

1 Comment on "Using Namespace Aliases"

Post a Comment

Leave a Comment

Comment Details
*
* BBCode: [b]bold[/b], [i]italics[/i], [code]code[/code], [li]bullet point[/li], [h]Heading[/h], [url="http://www.example.com"]link[/url], [quote author="John Smith"]quote[/quote]

Random Image

Hamster in a shoe

Hamster in a shoe (from the blog And So It Begins (part 2) )

Quick Poll

What is your DIP/IOC Container of choice?

  • 24.0% - Castle Windsor
  • 15.6% - StructureMap
  • 15.6% - Unity
  • 10.4% - Ninject
  • 9.7% - Spring.NET
  • 5.8% - Huh!?
  • 5.2% - None - DIP is the work of Satan!
  • 4.5% - Autofac
  • 4.5% - None - But I want to look into DIP
  • 2.6% - DIY (my own container)
  • 0.6% - PicoContainer.NET
  • 0.6% - LinFu
  • 0.6% - Other
  • 0% - Puzzle.NFactory
  • 0% - S2Container.NET

Thank you for voting!

View Comments (5) (See previous polls)

Latest Tweets

  • BTW: that last tweet was a joke, no actual money will be forthcoming

    about 1 hour ago from Echofon
  • I'll give £10 to anyone who retweets this message before 6pm

    about 1 hour ago from Echofon
  • RT: @ronskidoodles: OKAY OKAY OKAY!! The RT competition/freebie/stupid idea is now over!! @sironfoot is the last person to get one. - YAY!!!

    about 2 hours ago from Echofon
  • @ronskidoodles promises illustrations for anyone who retweets his new website. Gets over 210 RT (so far). Welcome to Twitter :-)

    about 2 hours ago from Echofon

View Dominic Pettifer's Twitter page.