Using Directives and Namespace in C#

1 minute read

UsingDirectivesMustBePlacedWithinNamespace is one of the rules of StyleCop and I was trying to figure out why StyleCop recommends having using directives defined within the namespace. 

Most of the books and articles (including mine) we have read do not really have using blocks within the namespace.  So is it really a good practice?  If yes, why?  Let’s check that in this article.

To illustrate the difference, I would choose a very simple example with 2 classes – Program (default one when a console application is created) and Environment (with the name same as System.Environment.)

  1. using System;
  2. namespace MyNS.Application
  3. {
  4.     internal class Program
  5.     {
  6.         private static void Main(string[] args)
  7.         {
  8.             Console.WriteLine(Environment.GetName());
  9.             Console.WriteLine(Math.Round(2.2, 0));
  10.         }
  11.     }
  12. }
  13.  
  14. namespace MyNS
  15. {
  16.     public class Environment
  17.     {
  18.         public static string GetName()
  19.         {
  20.             return "MyEnvironment";
  21.         }
  22.     }
  23. }

In the above example, when I refer to Environment on Line 8,  I am referring to my own class MyNS.Environment and not to System.Environment.  This is because the namespace System is defined outside the namespaces (NS and NS.Application).  In other words,

When the using directive is defined outside the namespace, the compiler will first search for the class (here, Environment) within the namespaces.  If it does not find the class locally (as for Math class on Line 9), it will try to search for the class in the references

So what happens if I move the using block inside the namespace (MyNS.Application)?  Will it break the above code?  Yes, because it will give precedence to System.Environment over MyNS.Environment and it will not find the method GetName in System.Environment

  1. namespace MyNS.Application
  2. {
  3.     using System;
  4.     internal class Program
  5.     {
  6.         private static void Main(string[] args)
  7.         {
  8.             Console.WriteLine(Environment.GetName());
  9.             Console.WriteLine(Math.Round(2.2, 0));
  10.         }
  11.     }
  12. }

So is the principle of StyleCop is correct, then it would break our code if we have used class names that are also part of the System namespaces (or in the referenced assemblies).  And it would require fixing by creating aliases like

  1. namespace MyNS.Application
  2. {
  3.     using System;
  4.     // Create an alias here
  5.     using Environment = MyNS.Environment;
  6.     internal class Program
  7.     {
  8.         private static void Main(string[] args)
  9.         {
  10.             Console.WriteLine(Environment.GetName());
  11.             Console.WriteLine(Math.Round(2.2, 0));
  12.         }
  13.     }
  14. }

This ensures that your code is StyleCop compliant and also refers to your own classes instead of System defined classes or those in the referenced assemblies

Updated: