Importing individual classes in .NET

The using statement in C# and the Imports statement in VB.NET don't let you import single classes the way you can in some other languages. For some people, having an entire namespace imported into a class seems untidy - its equivalent to doing something like import java.util.* in Java.

Once you start importing entire namespaces, clashes between type names become a real possibility, so the .NET import statements support an alias feature to work around this problem. For example if you had these three imports:

using System.Windows.Forms;
using System.Threading;
using System.Timers;

then a reference to Timer would be ambiguous, because all three of those namespaces provide a class called Timer. So .NET allows you to alias classes to different names so that you give them a new short name that doesn't clash, and doesn't need to be fully qualified. For example:

using System.Windows.Forms;
using System.Threading;
using System.Timers;
using TimerControl = System.Windows.Forms.Timer;

Now any references in code to TimerControl are unambiguously resolved to System.Windows.Forms.Timer.

This alias feature can be used to import single classes by simply aliasing them to their own name. For example:

using Button = System.Windows.Forms.Button;
using Control = System.Windows.Forms.Control;
using EventArgs = System.EventArgs;

Okay so it's a little ugly, and is probably something that only the most obsessive compulsive of neat freak programmer will want to do, but if you've come to .NET from another language, and you miss the ability to import individual classes, this is one way to achieve it.


Recent

Routing Audio Between Skype and Ableton Live on OSX

A couple of days ago I pwnd audio routing on OSX so that I can now conduct Skype interviews via Ableton Live.

Read more...

Posted 2009-10-23T09:45:40Z+0700
from Home, Sukhumvit, Bangkok #

Six Economics Video and Audio Podcasts

Posted 2009-09-15T23:31:22Z+0700
from Home, Sukhumvit, Bangkok #

Amalga HIS 5.3 Ship It!

My first Microsoft Ship It

Read more...

Posted 2008-12-01T21:12:23Z+0700
from Fraser Suites, Sukhumvit, Bangkok #

Tags

.net 808 ableton_live apple architecture art audio back_to_basics bamboo bangkok bumrungrad cambodia chinjoni coffee css cygwin debian desktop e.ii economics elements graphic_design hardware html hydrogen java krypton language linux mac media mediawiki microsoft music osx pdf pebble pebbleaddons podcasting podcasts ship singapore skype software_dev softwaredev soundflower sukhumvit sysadmin tellurium thailand thinkpad tools travel vmware wallpaper web wiki windows work x1seven

Older Posts

About

I'm a software professional who works mostly with Java and .NET technologies and who loves to ship. I've been writing software since I was around 6 or 7 when my parents bought a Commodore VIC-20 for the family.

I currently work as a SSDE with Microsoft's Healthcare Solutions Group in Bangkok, Thailand where we work closely with Bumrungrad International Hospital. I'm often also found working out of my home office in Melbourne, Australia.

Body of Work

Krypton - Industrial Age Software Builds

Krypton is a build tool that offers a build paradigm akin to an industrial assembly line.
website

Bamboo - Stress Free Page Layouts

Bamboo offers an alternative approach to implementing page layouts that eliminates the stress caused by CSS browser incompatibilities.
website / sourceforge

Other