Namespace
defines
the area of code. Namespace separate objects in different area. In
each area, there are some object define in it. Each area is different
from other and namespace prevent conflict between area.
The syntax of the namespace define below. Namespace is a keyword and one opening and closing bracket is necessary. In general, namespace content classes , but . Net architecture allow interface ,enumeration, deligate, structure etc.
namespace namespace_name
{
// class,structure ,enumeration etc.
}
Object under namespace can be accessed by using. First namespace keyword , then "." , then namespace name.
namespace_name.object;
Below is a example of the same. Below is a program, to show how a namespace is used in a program.
using
System;
namespace
Student1namespace
{
class
School1
{
public
void
ShowName()
{
Console.WriteLine("You
are in School1 namespace");
}
}
}
namespace
Student2namespace
{
class
School2
{
public
void
ShowName()
{
Console.WriteLine("You
are in School2 namespace");
}
}
}
class
Program
{
static
void
Main(string[]
args)
{
Student1namespace.School1
fc
= new
Student1namespace.School1();
Student2namespace.School2
sc
= new
Student2namespace.School2();
fc.ShowName();
sc.ShowName();
Console.ReadKey();
}
}
Output:
You are in School1 namespace
You are in School2 namespace
using keyword: using keyword declared at the top of the program followed by namespace name. You can use multiple using statement with multiple namespace. This will result, each object of the mention namespace will be available throughout the program. Syntax of the using statement is here
for example so now we have a namespace named "Student1namespace ". Here is the example, how this can be use.
using
System;
using
Student1namespace;
using
Student2namespace;
namespace
Student1namespace
{
class
School1
{
public
void
ShowName()
{
Console.WriteLine("You
are in School1 namespace");
}
}
}
namespace
Student2namespace
{
class
School2
{
public
void
ShowName()
{
Console.WriteLine("You
are in School2 namespace");
}
}
}
class
Program
{
static
void
Main(string[]
args)
{
School1
fc
= new
School1();
School2
sc
= new
School2();
fc.ShowName();
sc.ShowName();
Console.ReadKey();
}
}
Output:
You are in School1 namespace
You are in School2 namespace
Nested namespace : To namespace can be declared under another namespace. The innermost namespace object, you should use. Operator to access the hierarchy. The outermost namespace, followed by inner namespace, then the object name.
using
System;
namespace
Student1namespace
{
class
School1
{
public
void
ShowName()
{
Console.WriteLine("You
are in School1 namespace");
}
}
namespace
Student2namespace
{
class
School2
{
public
void
ShowName()
{
Console.WriteLine("You
are in School2 namespace");
}
}
}
}
class
Program
{
static
void
Main(string[]
args)
{
Student1namespace.Student2namespace.School2
fc
= new
Student1namespace.Student2namespace.School2();
fc.ShowName();
Console.ReadKey();
}
}
Output:
You are in School2 namespace
Namespace Alias : you can use in namespace as alias. For example, a namespace "myNamespace" can be alias as "ab". Below is the syntax of the same.
ab=myNamespace
Here is example of, how a namespace can be alias and use in program.
using
System;
using
alias1=Student1namespace;
using
alias2
= Student2namespace;
namespace
Student1namespace
{
class
School1
{
public
void
ShowName()
{
Console.WriteLine("You
are in School1 namespace");
}
}
}
namespace
Student2namespace
{
class
School2
{
public
void
ShowName()
{
Console.WriteLine("You
are in School2 namespace");
}
}
}
class
Program
{
static
void
Main(string[]
args)
{
alias1.School1
fc
= new
alias1.School1();
alias2.School2
sc
= new
alias2.School2();
fc.ShowName();
sc.ShowName();
Console.ReadKey();
}
}
Global Namespace : System is the root of all namespace. System contain base class and fundamental class generally commonly used. All namespace is comes under the system namespace. Now if I define a class named "system".The following you will be happen.
namespace
MyNamespace
{
class
Student
{
public
class
System
{ }
static
void
Main(string[]
args)
{
System.Console.WriteLine("Hello,
World!");
System.Console.ReadKey();
}
}
}
error Output :
'Student.System' does not contain a definition for 'Console'
"Console" comes under the System namespace , But system class is defined locally, it will super sit System namespace. System
namespace and function and will not be available . In this situation,
belowe "global" keyword is used to identify the Global namespace. Is example of
the same.
namespace
MyNamespace
{
class
Student
{
public
class
System
{ }
static
void
Main(string[]
args)
{
global::System.Console.WriteLine("Hello,
World!");
global::System.Console.ReadKey();
}
}
}
No comments:
Post a Comment