using – There’s more there than you are using
If you’ve spent more than a day programming in CSharp, you have already discovered the need for the using directive:
using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Net; using System.IO;
But are you aware of the other ways you can use this directive?
For example: If you have two classes with the same name but under two different namespaces and you need to refer to both of them in the same file, how would you do that?
You could do something like this:
using Namespace.one; namespace AutoTwit.Code { class Sample { public void foo() { ClassInOneAndTwo v = new ClassInOneAndTwo(); Namespace.two.ClassInOneAndTwo v2 = new Namespace.two.ClassInOneAndTwo(); // more code here... } } }
But if you have to do this too much, it becomes awkward at best.
The solution to this problem is to alias namespace two so that you can refer to it with a shorter name:
using Namespace.one; using T = Namespace.two; namespace AutoTwit.Code { class Sample { public void foo() { ClassInOneAndTwo v = new ClassInOneAndTwo(); T.ClassInOneAndTwo v2 = new T.ClassInOneAndTwo(); // more code here... } } }
Or if you want to make it even shorter, you can alias the class:
using Namespace.one; using T = Namespace.two.ClassInOneAndTwo; namespace AutoTwit.Code { class Sample { public void foo() { ClassInOneAndTwo v = new ClassInOneAndTwo(); T v2 = new T(); // more code here... } } }
Just think of all the career secure code you could write with this!
Seriously, though. It has its place. But I could see someone being cute and renaming all the classes they use to one- and two-character names just so they “don’t have to type so much.”
Other post in Advanced CSharp
- Two Interfaces. Same Method. Two meanings. - September 29th, 2008
- Making values nullable - October 9th, 2008
- CSharp's Property Shortcuts - October 23rd, 2008
- Readonly variables in CSharp? Really?! - October 29th, 2008
- Dispose with Using - November 10th, 2008
- Delegates in .NET - December 4th, 2008
- Using Sealed in CSharp - December 8th, 2008
- CSharp checked and unchecked - December 11th, 2008
- Advanced CSharp - unsafe mode - December 15th, 2008
- Volatile variables and CSharp threads - December 22nd, 2008
- What is the global keyword in CSharp? - December 29th, 2008
- CSharp fixed keyword - January 5th, 2009
- using - There's more there than you are using - February 2nd, 2009
- Stackalloc in CSharp - February 16th, 2009
- Removing Warnings from CSharp Compile Cycle - March 10th, 2009
- && vs & and | vs ||... What's the difference? - March 16th, 2009
- Advanced CSharp - yield - March 25th, 2009
- Just say “No!” to C# Regions? Really?! - April 16th, 2009
- C# “” better than string.Empty? - April 20th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- CSharp ?? Operator - May 18th, 2009
- Using VB.NET From CSharp - July 1st, 2009
- Dispose, Finalize and SuppressFinalize - July 9th, 2009
- What is .NET’s Object.GetHashCode() Used For? - August 5th, 2009
- ASP.NET Substitution Control - October 22nd, 2009
Other Related Items:
Maxsa Solar Dual Head LED Security Floodlightilluminates doorways, walkways, or garages, at night when motion is detected * dual-head design creates two light beams using 8 bright LEDs (4 per hea... Read More >
Keyboard Town PALS Little Hands Can Type Preschool Readiness Program, Nov. 2009, CD-ROM, PC & Mac Compatible with Flash Player Version 8 or Later Installed, Is Designed to Introduce Pre-school Age Children to 7 Basic Computer Keyboarding Concepts with the Aid of Puppets, Poetry & Fun Activities. This Product Will Lay the Essential Foundation Needed for Preschoolers to Proceed to the Keyboard Town PALS, Learn to Type Software; Teaching Kids to Type Using Proper Technique in an Hour. Available on Amazon.The reality is that young children are on the computer and until now parents and educators thought that preschoolers were not capable of learning prop... Read More >










