For the past eight(8) years Schalk Neethling has been working as a freelance developer under the pseudo of Volume4 and is now the president of Overt Strategy Consulting. During this period he has completed over 300 projects ranging from full web application development to complete branding. As president and lead developer of Overt Strategy Consulting, Schalk Neethling and his team has released a 100% Java standards based content management system called AlliedBridge and business document exchange and review system, called Doc-Central. Schalk Neethling is also actively involved on a daily basis in the open source, web standards and accessibility areas and is a current active member of the Web Standards Group. Schalk is also the co-founder and president of the non-profit The South Web Standards and Accessibility Group, which aims to actively educate and raise awareness of web standards and accessibility to both the developer society as well as business large and small. Schalk also has a long relationship with DZone and is currently zone leader for both the web builder, css.dzone.com, as well as the .NET zone, dotnet.dzone.com, and you can find a lot of his writing there as well as on his blog located at schalkneethling.alliedbridge.com. Schalk is constantly expanding on his knowledge of various aspects of technology and loves to stay in touch with the latest happenings. For Schalk web development and the internet is not just a job, it is a love, a passion and a life style. Schalk has posted 173 posts at DZone. View Full User Profile

Another Use for C# Using

06.16.2008
| 12626 views |
  • submit to reddit

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;

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.

Original Author

Original Article authored by Michael Wood

References
Published at DZone with permission of its author, Schalk Neethling. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)