NAMESPACE AND ITS TYPES
Namespace is used to organize the types such as classes. It helps to control the scope of the classes and other types in the project. Namespaces are heavily used in C# in two ways. - First, .NET uses namespaces to organize its many classes which are in the .NET Framework. For example, the System.Collections namespace is a collection of specialized classes representing different data structures to store and retrieve data. Another level of organizing such types is the System.Collections.Generic namespace.
- Second, declaring namespaces by the developers in their project help them to control the scope of class and method names in larger programming projects.
NAMESPACES ARE CONVENIENCE
It is not mandatory to keep a class inside a namespace. For example, the following code is without namespace which can be compiled successfully. But we must use namespace for code organization.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Class without namespace...");
}
}
The default name of namespace in a project is the name of the project. For example, if the project name is ConsoleApp1, then we get the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
}
}
}
But we can rename the above namespace. It is not mandatory to have the namespace name as that of the project. The following code will also compile successfully.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyNamespace
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Namespace rules");
Console.ReadKey();
}
}
}
NESTED NAMESPACES
A namespace can nest another namespace as well. Following code will compile successfully.
using System;
namespace ConsoleAppxxx
{
namespace First
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Nesting namespace rules");
Console.ReadKey();
}
}
}
DOT OPERATOR TO NEST
The dot operator can be used to nest the namespaces. Following code will compile successfully.
using System;
namespace ConsoleAppxxx.First
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Nesting namespace rules");
Console.ReadKey();
}
}
}
BRACES OPERATOR TO NEST
The following example illustrates the use of nested namespaces using braces. The Second namespace is inside the First namespace. Both namespaces have the same name class called Program but their is no name conflict because of namespace. The namespace provides the scope of each name. To access the class of the outer namespace First, we have used the dot operator inside the inner namespace Second. The code compiles successfully and the output is shown also.
using System;
namespace First
{
class Program
{
public void Run()
{
Console.WriteLine("Inside First");
}
}
namespace Second
{
class Program
{
static void Main(string[] args)
{
First.Program fp = new First.Program();
fp.Run();
Console.WriteLine("Inside Second");
Console.ReadKey();
}
}
}
}
OUTPUT
Inside First
Inside Second
Independent Namespaces
We can also have independent namespaces in a class file. This is exemplified below.
using System;
namespace First
{
class Program
{
public void Run()
{
Console.WriteLine("Inside First");
}
}
}
namespace Second
{
class Program
{
static void Main(string[] args)
{
First.Program fp = new First.Program();
fp.Run();
Console.WriteLine("Inside Second");
Console.ReadKey();
}
}
}
OUTPUT
Inside First
Inside Second
USING DIRECTIVE
The using directive is used with regard to namespace. For example in the above code, using System; statement is used. This is used to access the types (e.g. Console) of System namespace without fully qualifying the namespace in the code. For example, Console class used in the code is found in the System namespace. What if, Console class is also existing in another namespace. Then, name conflict will occur and the compiler will fail to compile. So, it is necessary to use the directive using to direct the compiler that such and such Namespace and its types/classes are used in this file.
The using statement is also used when the nested namespace is very lengthy and then an alias is more appropriate to use for such nested namespace. For example, let there are four namespaces and they are nested and the final namespace invocation is Primus.Software.Project.Dummy. Inside the Dummy namespace, suppose class Book exits. and it has Details() method. Then to access Details in any other class, we will have to fully qualify like
Primus.Software.Project.Dummy.Book bk = new Primus.Software.Project.Dummy.Book();
bk.Details();
ALIASING NAMESPACE
To avoid such kind of lengthy typing, we use the alias of a namespace. For example, using dummy = Primus.Software.Project.Dummy; //alias
According to Microsoft site, namespaces have the following properties:
- They organize large code projects.
- They're delimited by using the . operator.
- The using directive obviates the requirement to specify the name of the namespace for every class.
- The global namespace is the "root" namespace: global::System will always refer to the .NET System namespace.
RULES OF NAMESPACE
1. The outer namespace names can be used in the inner namespace without qualifying it with the outer namespace.
Example
namespace Outer
{
class Class1 {}
namespace Inner
{
class Class2 : Class1 {}
}
}
2. The name of one independent namespace into another namespace must be qualified with the namespace.
namespace ProjectA
{
class Class1 {}
}
namespace ProjectB
{
class Class2 : ProjectA.Class1 {}
}
3. If same name appears in outer namespace and the inner namespace, the inner name can be used in its namespace without qualifying but using outer name inside the inner will require qualifying the outer namespace.
Example
using System;
namespace Outer
{
class Program
{
public void Run()
{
Console.WriteLine("Inside Outer");
}
}
namespace Inner
{
class Program
{
static void Main(string[] args)
{
Outer.Program fp = new Outer.Program();
fp.Run();
Console.WriteLine("Inside Inner");
Console.ReadKey();
}
}
}
}
Some More examples:
Example1 Independent Namespaces
using System;
namespace WebProject
{
public class javaTeam
{
public void Display()
{
Console.WriteLine("Java Team");
}
}
public class DotNet
{
public void Display()
{
Console.WriteLine("Dotnet Team");
}
}
}
namespace DesktopProject
{
public class Windows
{
public void Display()
{
Console.WriteLine("Windows Team");
}
}
public class Linux
{
public void Display()
{
Console.WriteLine("Linux Team");
}
}
}
namespace Console_NameSpaceExammples
{
class Program
{
static void Main(string[] args)
{
//You cannot access Java or Dotnet class inside this namespace unless you use their namespace called WebProject
WebProject.javaTeam java = new WebProject.javaTeam();
java.Display();
}
}
}
Example2 Inner Namespaces
using System;
namespace Console_NameSpaceExammples
{
namespace WebProject
{
public class javaTeam
{
public void Display()
{
Console.WriteLine("Java Team");
}
}
public class DotNet
{
public void Display()
{
Console.WriteLine("Dotnet Team");
}
}
}
namespace DesktopProject
{
public class Windows
{
public void Display()
{
Console.WriteLine("Windows Team");
}
}
public class Linux
{
public void Display()
{
Console.WriteLine("Linux Team");
}
}
}
//Program class namespace holds two other namespaces as inner namespaces namely WebProject and DesktopProject. Since Program class is not part of these namespaces. It cannot directly access their members
class Program
{
static void Main(string[] args)
{
//You cannot access Java or Dotnet class inside this namespace unless you use their namespace called WebProject
javaTeam java = new WebProject.javaTeam();
java.Display();
}
}
No comments:
Post a Comment