When many people work in creating the same program, it could be difficult to keep track of the names of various classes. If more than one programmer creates a class with the same name in the same program, there would be conflict and the program would not work. The solution to avoid this situation is to delimit sections of code with names.
A names pace is a section of code that is identified by a specific name. The name could be anything such as somebody’s name, the name of the company’s department, or a city. To create a namespace, you start with the namespace keyword followed by the name of the section.
Like a class, the section that is part of a names pace starts with an opening curly bracket “{“and ends with a closing curly bracket “}”.Here is an example:
namespace Jason
{
}
Between the curly brackets, you can type anything that is part of the namespace. For example, you can create a class inside of a namespace. Here is an example:
namespace Jason
{
class Airport
{
}
}
We’ll be covering the following topics in this tutorial:
Accessing members of Namespace
After creating the necessary items in a names pace, you can use the period operator (.) to access an item that is part of the namespace. To do this, in the desired location, type the name of the names pace, followed by a period, followed by the desired member of the namespace. Here is an example:
namespace Accounting
{
class Departure
{
}
}
class Mainclass
{
static void Main)
{
Accounting.Departure
}
}
Namespace Nesting
Imagine that you create a namespace called Accounting and many people from the Accounting department are asked to create their methods and objects only in the Accounting names pace and you may have asked the Accounting programmers not to create their methods and objects outside of the Accounting namespace. One thing you can do is to create other names paces inside of the existing namespace.
Creating a namespace inside of an existing namespace is referred to as nesting the namespace. The namespace inside of another namespace is nested.
To create a namespace inside of another, simply type it as you would create another namespace. Here is an example:
namespace Accounting
{
namespace Jason
{
}
}
In the example above, the Jason namespace is nested inside of the Accounting namespace.
After creating the needed namespaces, nested or not, you can create the necessary methods and objects inside of the desired namespace. To access anything that is included in a nested namespace, you use the period operator before calling a member of a namespace or before calling the next nested namespace. Here is an example:
namespace Accounting
{
class payroll
{
}
}
namespace Personnel
{
namespace EmployeesRecords
{
class Timesheet
{
}
}
namespace Benefits
{
class Medicallnsurance
{
}
}
}
class Mainclass
{
static void Main()
{
Accounting. payroll
personnel.Benefits.Medicallnsurance
personnel.EmployeesRecords.TimeSheet
}
}
A line of code such as Accounting.Payroll is called a statement. Every statement must be terminated with a semicolon. Therefore, this statement may be written as:
Accounting.Payroll;
The System Namespace
To make programming in C# a little easier, many classes have already been created and stored in various namespaces. Each namespace in C# is used to provide a specific set of classes. The most regularly used namespace in the C# language is called System. Inside of the System namespace is a class called Console. The Console class is used to display things on the console screen also called the DOS window.
The Console class contains assignments (called methods) to display information on the screen or to retrieve information from the user who types it in the DOS window. The method that is used to display text on the screen is called Write. To use the Write() method, inside of the parentheses, type the sentence between double-quotes. Here is an example:
class program
{
static void Main()
{
System.console.write(“The Wonderful world of c# programming”);
}
}
Besides the Write() method, the Console class also provides a method called WriteLine(). The difference is that, after displaying something on the screen, the Write() method keeps the caret (cursor) on the same line but WriteLine() transfers the caret the next line.
The using Keyword
We saw that, to call an object or a method that is part of a namespace, you must “qualify” that method or object using the period operator. Instead of using this approach, if you already know the name of a namespace that exists or has been created in another file, you can use a special keyword to indicate that you are using a names pace that is defined somewhere. This is done with the using keyword. To do this, on top of the file (preferably), type using followed by the name of the namespace.
With the using keyword, you can include as many external namespaces as necessary.
You can use ‘=’ operator with using key word to assign the alias name for the any namespace see an example
using X = System.console;
after this statement you can use alias ‘X’ in place of ‘System. Console’ like this:
x.writeLine (“Hello”)
x.writeLine (“HoWare you”);
or
X.ReadLi ne();