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 standing next to a tiny hard drive.

Tiny hard drive or GIANT hamster? You decide! (from the blog And So It Begins )

Quick Poll

What is your DIP/IOC Container of choice?

Poll Vote
(see results)
View Comments (0) (See previous polls)

Latest Tweets

  • Red Bull gives you wings....that generate huge amounts of downforce #F1

    about 18 hours ago from Twitterrific
  • .vampire { -webkit-box-shadow: none; -webkit-box-reflection: none; } #cssjokes

    7:44 PM July 30th from Echofon
  • @edhenderson lol, lets get a trending topic going - .gangster .wrapper { color: #000; width: 150%; text-decoration: bling; } #cssjokes

    7:36 PM July 30th from Echofon
  • @weblivz I think the petition should be resubmitted but with security stuff taken out, as that's what the response purely focused on

    6:13 PM July 30th from Echofon
  • @weblivz I still think Chrome Frame can come to the rescue here, still keep their old browsers + legacy systems, no retraining costs etc.

    6:12 PM July 30th from Echofon

View Dominic Pettifer's Twitter page.