.NN #18: Another use for Using

If you are a C# developer you’re familiar with the using directive.  It appears at the top of every one of your class files informing the compiler what namespaces should be looked at while compiling this code page so that you don’t have to fully qualify every object. (This isn’t the interesting bit)

using System;
using System.Collections.Generic;

But did you also know you can use it to make aliases? (This still isn’t the interesting bit, keep reading…)

using Foo = System.Diagnostics;

This would allow code to say Foo.Trace or Foo.Debug.  Aliases are used most often to deal with namespace collisions where two namespaces contain an object with the same name.  You can provide an alias to one of the namespaces and then use the alias with the object declaration.  Note that you can alias not just a namespace, but a type as well.

The interesting bit is what I ran across in the Enterprise Library code.  They were using aliases along with conditional pre-compiler directives to be able to use either NUnit or MSTest in their unit tests without having the change code.  Here’s an example:

using System.Configuration;
using SysConfig = System.Configuration;
#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif
 
namespace LongNameRemovedForSpace.Logging.Tests
{
      [TestClass]
      public class LoggerFixture
      {
            
            [TestInitialize]
            public void SetUp()
            {
                  Logger.Reset();
                  MockTraceListener.Reset();
                .....

Notice how they have a #if pre-compiler directive set up that either sets a using for the MSTest Unit testing namespace or, if the !NUNIT is set to false, then it pulls in a series of namespaces aliasing the attribute names for NUnit to the corresponding attribute name for the MSTest framework.  This means that the code file will compile using one set of attribute names for their unit tests, but via a compiler constant you can switch to run those unit tests under NUnit or MSTest.  That’s pretty slick.

I posted this as a .NET Nugget because I think it is a cool use of the using directive.   When they put this together they had to solve the problem of allowing for people to run unit tests under both NUnit and MSTest.  Rather than having duplicate code files everywhere or tons of #if - #else pre-compiler directives around all the attributes they found a simple way to solve the problem.