Home » Advanced CSharp » using – There’s more there than you are using

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

Related Post

  • Dispose with UsingDispose with Using I'm sure that many of you already know that many of the objects in the .NET framework need to be disposed.  The most common of these are the windows objects and the stream objects.Of […]
  • Delegates in .NETDelegates in .NET I received the following question:What is a delegate?  What problem does it solve?  and When might I use a delegate?A delegate is essentially a function pointer.  We have used […]
  • Advanced CSharp – yieldAdvanced CSharp – yield Have you ever had a situation arise where you want to create a function that returns a collection of results and you want the results to be listed in a for each loop?  Sure you […]
  • Readonly variables in CSharp? Really?!Readonly variables in CSharp? Really?! I'm sure most of you are familiar with the keyword "const," which allows you to declare a variable and give it a value and assures that no other code will change the value. const int v = […]
  • CSharp adds the var keyword!CSharp adds the var keyword! There have been several new features added to the CSharp language that will significantly reduce the amount of code that ends up in our source files.  It will not significantly reduce the […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.