Introduction of Namespaces and Base Classes in C#

In C#, namespaces are used to logically arrange classes, structs, interfaces, enums and delegates. The namespaces in C# can be nested. That means one namespace can contain other namespaces also.
 
The .NET framework already contains number of standard namespaces like System, System ,Collections, and System.Drawing etc. In addition to these standard namespaces, the user can define its own namespaces in C#..
 
A namespace can be seen as a container for some classes in much the same way that a folder on your file system contains files. Namespaces are needed because there are a lot of .NET classes. Microsoft has written thousands of base classes, and any reasonably large application will define many more.
 
By putting the classes into namespaces we can group related classes together, and also avoid the risk of name collisions: If your company happens to define a class that has the same name as the class written by another organization, and there were no namespaces, there would be no way for a compiler to figure out which class a program is actually referring to.
 
With namespaces, there isn’t a problem because the two classes will be placed in different namespaces, which compares with, say, the Windows files system where files with the same name can be contained in different folders.
 
It is also possible for namespaces to contain other namespaces, just as folders on your file system can contain other folders as well as files.
 
Creating a Namespace
 
The C# language provides a keyword namespace to create a user defined name space. The general form of declaring a namespace is:
 
namespace 
{
 	// Classes and/or structs and/or enums etc.
}
 
The name of namespace can be any valid C# identifier or combinations of identifiers separated by commas (,).
 
testNamespace.cs
 
using System;
namespace Ujjwal.Csharp.Codes
{
 	class MyClass
 	{
 		public MyClass()
 		{
 			Console.WriteLine("My Class");
 		}
 	} 
 	class MyClient
 	{
 		public static void Main()
 		{
 			MyClass mc = new MyClass(); // Displays 'My Class'
 		}
 	}
} 

 
It is not possible to use any access modifiers like private, public etc with namespace declarations. The namespaces in C# are implicitly having public access and this is not changeable.
 
The namespace elements can’t be explicitly declared as private or protected. The namespace allows only public and internal elements as it members. The default is internal. The following code doesn’t compile in C#, since the class inside the namespace is declared as private.
 
namespace Ujjwal.Csharp.Codes
{
 	private class MyClass
 	{
// Body of the MyClass
}
} 
 
Accessing Namespace Members
 
The namespace members can be accessed by using a fully qualified name, which including the namespace name and member name separated by dot(.) from outside the namespace.
 
For example:
 
using System;
namespace Ujjwal.Csharp.Codes
{
 	class MyClass
 	{
 		public MyClass()
 		{
 			Console.WriteLine("My Class");
 		}
 	}
} 
class MyClient
{
 	public static void Main()
 	{
 		//Using the fully qualified name to access the namespace member.
 		Ujjwal.Csharp.Codes.MyClass mc = new 
          Ujjwal.Csharp.Codes.MyClass();
 	}
} 
 
Instead of typing fully qualified name every time, the C# provides another keyword using to define some aliases to namespaces. Then while writing our code, we just refer the classes with their class name only. During compile time, the compiler will map all the class names with aliases to reach at the fully qualified name of the class. Once the fully qualified name has been found, it is used to convert the code to IL code.
 
Remember that in IL code all classes, interfaces, enums and delegates are referenced with their fully qualified name.
 
An example of using is shown below.
 
using Ujjwal.Csharp.Codes;
 	MyClass mc = new MyClass(); 

Remember that inside C# namespaces,
 it is possible to use the keyword using. 
namespace MyNameSpace
{
 	using System;
 	class MyClass
 	{
 	//Body of MyClass
 	}
}
 
Any code contained within namespace MyNameSpace has access to System namespace without fully qualifying the type names, but those types are not members of the MyNameSpace namespace.
 
The same namespace can have the size over multiple lines as follows.
 
using System;
namespace Ujjwal.Csharp.Codes
{
 	class MyClass
 	{
 		public MyClass()
 		{
 			Console.WriteLine("My Class");
 		}
 	}
} 
namespace Ujjwal.Csharp.Codes
{
 	class MyClass1
 	{
 		public static void Main()
 		{
 			MyClass mc = new MyClass();
 		}
 	}
} 
 
Creating Aliases
 
With the help of the keyword using, it’s possible to create an alias name for a namespace or type.
 
For example
 
using con = System.Console; // Create an alias
class MyClient
{
 	public static void Main()
 	{
 		con.WriteLine("Hi, Ujjwal! How are you?");
 	}
} 
 
Problems with Namespaces in 1.1
 
If you worked on a large project in .Net in the past, you must have faced some problems with Namespaces. Namespaces were introduced in .Net to eliminate the naming collisions between different codes developed at different locations. But as large and large projects were developed, there were some shortcomings were observed. Some of them included:
 
a. Same typename can be used in multiple namespaces. In this scenario, types in local namespaces can mask types in global namespaces. And this leads to an ambiguous situation.
 
b. Same typenames can be used in multiple assemblies. When these assemblies were used in the same application, again, there will be ambiguity in referencing the types.
 
c. using aliases can lead to ambiguous identification. Aliases can mask the type if not taken care.