Namespaces, using Keyword and Referencing Assemblies
Add Comment<span class="wboxheado">Introduction</span><br> In this article I will explain what are <b>Namespaces</b> and why they should be used. Later I will explain the use of the <b>using</b> keyword and the need for referencing assemblies while compiling. <br> <br> <span class="wboxheado">Naming Problem</span><br> When a language develops, many third party components are available. All these parties try to give meaningful names to their classes like the Math class, or IntrestCalculator class etc. We as developers end up in misery due to this, just consider an<font color="#FF0000"> </font>example; <br> I am a developer who is writing up a shopping cart object. I am using third party components, now I purchase two such components, one calculates discount rates for retail customers and the other calculates discount rates for wholesale customers. The problem arises when both these components have a class called <i>Discount</i>. Now in my shopping cart class how do I use both these classes unambiguously?<br> Example:<br> <br> <span class="codetext">Discount d1 = new Discount(); <br> //Which component does the compiler here refer to? <br> //Reseller discount or Wholesaler Discount?<br> int discount = d1.Cal(45.78) ;</span><br> <br> Some compilers will complain while others might just compile and use any of the components of their wish!<br> <br> <span class="wboxheado">Solution</span><br> This problem has been identified and solved by Microsoft on the .NET Platform with the use of <i>Namespaces</i>. The relationship between Namespaces and classes can be directly compared to the relationship between Files and Folders on your computer. Files actually contain data, while Folders are used to just manage and logically arrange Files. Folders can also contain many files and sub folders within them. <br> In the same way Classes actually contain data and Namespaces are used to logically arrange classes. Namespaces can also contain many other Namespaces and classes. <br> This concept may seem similar to the <b> Package - Class</b> relationship used in Java. But one point of caution, you <b> DO NOT</b> need to create <b> folders</b> to store classes into a Namespace. Just defining the <i>Namespace</i> keyword above the class definition is enough. <br> <br> But again you might say that there are 2 companies whose abbreviated names might be XYZ and if both companies develop the <i>Discount</i> class then, even if they used namespaces their full names would be <i>XYZ.Discount</i> class? Which will lead to the problem that we faced earlier! This is very true since Namespaces only help to extend the name of a class to make it unique. Hence, Microsoft is encouraging companies to use their full names to develop components and not abbreviations also if possible the departments name as well as the teams name should be used.<br> So say a company like American Business Company should use it full name AmericanBusinessCompany.It.DotNet.Discount instead of using ABC.Discount. This will help to solve the naming conflict.<br> <br> <span class="wboxheado">Namespace Usage</span><br> To use namespaces all you have to do is place the namespace definition above your class definition. The dot '.' (Scope Operator) is used to denote and access classes within the namespace.<br> example:<br> Say I have a class Namer<br> <br> <span class="codetext">public class Namer{<br> public int i=10 ;<br> }</span><br> <br> To place this class within a Namespace<br> <br> <span class="codetext">namespace Saurabh.Csharp.Examples<br> {<br> public class Namer {<br> public int i=20;<br> }<br> }</span><br> <br> Now lets see how we will access the above two classes.<br> <br> <span class="codetext">namespace Saurabh.Csharp.Examples<br> {<br> public class NameConsumer {<br> public static void Main()<br> {<br> Namer n1 = new Namer(); //access the plain Namer class<br> Saurabh.Csharp.Examples.Namer n2 = new Saurabh.Csharp.Examples.Namer();<br> System.Console.WriteLine("Just Namer value is :"+n1.i ); //This will print 10<br> System.Console.WriteLine("Namespaced Namer value is :"+n2.i) ; //This will print 20<br> }<br> }<br> }</span><br> <br> <span class="wboxheado">using Keyword</span><br> The <b>using</b> keyword can be mapped to the <b> import</b> keyword in Java. For people from other languages I would say simply that <i> using</i> keyword is used to <b> alias</b> a class or Namespace. All the compilers on the .NET platform emit IL (intermediate language) code on compilation, in which all the classes are referenced with their <i> fully qualified names</i> i.e. the full namespace as well as the class name. This would mean that developers would always have to use fully qualified names for all their classes!<br> If you<font color="#FF0000"> </font>would see the above class NameConsumer, I have referenced all the classes using their fully qualified names, this causes a lot of typing to be done! <br> In order to save the developers from typing fully qualified every time, C# has the <b> using</b> keyword. We use the <i>using</i> keyword in the beginning of the class to define some aliases to Namespaces. Then while writing our code we just refer the classes with their class name only. <br> During compile time the compiler takes additional burden to try to map all the class names with the aliases that you have defined to reach at the fully qualified name of the class. Once the fully qualified name has been found it is then used to convert the code to IL code.<br> One important point worth mentioning is that, if you are using classes with<font color="#FF0000"> </font>the<font color="#FF0000"> </font>same names but from different Namespaces then its better to use fully qualified names in your code to refer to those classes. That is why in the above NameConsumer class where I am using two classes which have the same name <b>Namer</b>, I use fully qualified names to reference classes in my code.<br> <br> Example<br> <span class="codetext">using System ;<br> using Saurabh.Csharp.Examples ;<br> public class SecondConsumer {<br> public static void Main(){<br> Namer n1 = new Namer();<br> Console.WriteLine(n1.i) ;<br> }<br> }</span><br> <br> In the above code I first define some aliases using the <i> using </i> keyword to reference the <i>System</i> and <i>Saurabh.Csharp.Examples</i> Namespaces.<br> During compile time when the compiler notices a reference to the <i>Namer</i> class it will try to get its fully qualified name. It will first try the <i>System.Namer</i> combination and check the <i>System</i> assembly if there is any class called <i>Namer</i>. Since there is no such class, it will then try the <i>Saurabh.Csharp.Examples.Namer</i> combination, this will pass since the <i>Namer</i> class is contained within the <i>Saurabh.Csharp.Examples</i> Namespace. The compiler will then map all the references to <i>Namer</i> class as references to <i>Saurabh.Csharp.Examples.Namer</i> class in the IL code. <br> <br> You can also use the <i>using</i> keyword to define references to fully qualified classes with your own alias. <br> example<br> <br> <span class="codetext">using System ;<br> using MyClass = Saurabh.Csharp.Examples.Namer ; //Here we put the path up to the class ! <br> public class ThirdConsumer{<br> public static void Main()<br> {<br> MyClass mc = new MyClass(); //This actually makes a instance of the Namer class<br> Console.WriteLine("The value is :"+mc.i) ;<br> }<br> }</span><br> <br> The above class shows how to define your own aliases. I have made a alias called <i>MyClass</i> to reference the class <i>Saurabh.Csharp.Examples.Namer</i>. Now when the compiler will reference the <i>MyClass</i> it will replace it with <i>Saurabh.Csharp.Examples.Namer</i> in the IL code. <br> <br> <span class="wboxheado">Referencing Assemblies</span><br> This topic does not fit here it should come under compiling, but I have placed it here to make people understand why should they reference assemblies during compilation. <br> As I have explained above the <i>using</i> keyword is just used to provide an alias to a Namespace or a class. It actually does not import any <i>MetaData</i> or headers unlike C / C++ where headers used to actually be imported by placing statements like <br> #include <stdio.h> <br> <br> Hence during compilation how can the compiler come to know which assemblies you have used and how can the compiler map the functions and class names ?<br> To enable to compiler to know where it can find the assembly which contains all the Namespaces and classes you have used, you have to reference those assemblies during compilation with the <b> /Reference</b> or <b> /r </b> compiler switch. If you fail to reference all the assemblies you have used, the compiler cannot map the classes and Namespaces and a <b> CS0234 Error </b> "The type or Namespace "XXX" does not exist in the class or Namespace "XXX" is raised.<br> <br> <i> So how do you know which assemblies should you reference ?</i><br> A simple way would be to look at the <b> using</b> keyword, check all the Namespaces you have aliased and in which assembly (library dll) does it exist. To find this out start the Reference documentation from "S<i>tart -> Program Files -> Microsoft.NET Framework SDK-> Reference Documentation</i>". <br> Go to the "<i>Reference -> Class Library</i>" section, then check out which Namespaces what you have used. The reference documentation contains information about the assemblies/ libraries that contain the Namespace. <br> <br> Once you have figured out the list of assemblies that you have to reference, use the <i>CSC</i> C# compiler and while compiling add the reference to all the assemblies. <br> <br> <span class="wboxheado">Conclusion</span><br> This article should solve many of your doubts on what Namespaces are and why do you need to reference Dll's while compiling.