As we’ve seen briefly in a previous example, namespaces in C# can give a project some useful ways to organize code. The most common example of where namespaces are useful is what I’ve dubbed the “System” example. A quick google on C# namespace will land you a ton of results showing a basic example. Lets go over a common basic example, and talk about some of the features to resolve conflicts.
First, lets explain what a namespace is. In my previous article, I described a namespace as a tool shed that can house many tools. If you exist in the too shed you have the ability to use a hammer object or a saw object that exist in the same shed. You can have multiple sheds, and even sheds within sheds. Now that you understand the concept here is the basic example:
using System;
class MyCoolApp
{
public class System { }
public static void Main()
{
System.Console.WriteLine("Write me!");
}
}
You should notice that the namespace you are using is System. You will also notice that the class name is System as well. If you were to compile this you would get an error:
MyCoolApp.cs(8,24): error CS0117: `MyCoolApp.System' does not contain a definition for `Console' MyCoolApp.cs(4,18): (Location of the symbol related to previous error) Compilation failed: 1 error(s), 0 warnings
You can see that the class System inside of MyCoolApp is found before the System namespace or, as Microsoft puts it, “hidden by the class” MyCoolApp.System.
There is a quick feature to get around this issue, and is usually refered to as the global:: operator. This gives a programmer access to specify the global namespace. In other words, this will give us access to C# System namespace instead of trying to overriding the System call with our own System class.
To use the global:: operator change the above example to:
using System;
class MyCoolApp
{
public class System { }
public static void Main()
{
global::System.Console.WriteLine("Write me!");
}
}
This modified example should now be able to compile run. There is also another feature of namespaces that could help with avoiding this issue and improve the readability of your code. That feature is called aliasing, and, like the global:: operator, you can refer to classes inside the namespace using the double colon notation.
Say you want to create an alias of the default C# System namespace and give it the name SysAlias. You could rewrite the example above to look like this:
using SysAlias = System;
class MyCoolApp
{
public class System { }
public static void Main()
{
SysAlias::Console.WriteLine("Write me!");
}
}
Notice that the global System.Console can be reached properly using this method. This may not look very helpful at first glance, but it can really help if you have many levels of namespaces. For example, you can improve code readability by aliasing the following class structure:
Transportation.Vehicles.Automobiles.FourDoor.ManualTransmission
to be referenced by an alias like “FourDoorManual” if you use the following:
using FourDoorManual = Transportation.Vehicles.Automobiles.FourDoor.ManualTransmission;
In the end, namespaces will allow you to eliminate the need to fully qualify classes such as System.Console or Transportation.Vehicles.Automobiles.FourDoor.ManualTransmission, and they offer a nice way of organizing code. I can see this being a very useful coding technique for keeping your code understandable, without sacrificing. I can even see it being a great feature to use when you want to debug or unit test your code (think modifying your namespace to use a simulator framework…). Try it out and let us know if you find some more interesting uses!



(Click to Rate!)












November 24th, 2008 at 7:28 pm
You’re totally awesome.
Thanks,it’s very helpful for me.
November 24th, 2008 at 10:21 pm
Great news, I’m glad it helps! Thanks for stopping by!