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.


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