C # Namespace (Namespace)
The design purpose of the namespace is to provide a way to separate a group of names from other names. The names of classes declared in one namespace do not conflict with the names of the same classes declared in another namespace.
Define namespace
The definition of a namespace begins with the keyword namespace, followed by the name of the namespace, as follows:
namespace namespace_name
{
// code declaration
}
In order to call a function or variable that supports a namespace version, the name of the namespace is placed in front, as shown below:
namespace_name.item_name;
The following program demonstrates the use of namespaces:
using System;
namespace first_space
{
class namespace_cl
{
public void func ()
{
Console.WriteLine ("Inside first_space");
}
}
}
namespace second_space
{
class namespace_cl
{
public void func ()
{
Console.WriteLine ("Inside second_space");
}
}
}
class TestClass
{
static void Main (string [] args)
{
first_space.namespace_cl fc = new first_space.namespace_cl ();
second_space.namespace_cl sc = new second_space.namespace_cl ();
fc.func ();
sc.func ();
Console.ReadKey ();
}
}
When the above code is compiled and executed, it produces the following results:
Inside first_space
Inside second_space
using keyword
The using keyword indicates that the program uses the name in the given namespace. For example, we use the System namespace in the program, which defines the class Console. We can just write:
Console.WriteLine ("Hello there");
We can write the fully qualified name as follows:
System.Console.WriteLine ("Hello there");
You can also use the using namespace directive so that you do n’t need to prefix the namespace name when using it. This directive tells the compiler that subsequent code uses the names in the specified namespace. The following code delays the application of the namespace.
Let's use the using specification to rewrite the above example:
using System;
using first_space;
using second_space;
namespace first_space
{
class abc
{
public void func ()
{
Console.WriteLine ("Inside first_space");
}
}
}
namespace second_space
{
class efg
{
public void func ()
{
Console.WriteLine ("Inside second_space");
}
}
}
class TestClass
{
static void Main (string [] args)
{
abc fc = new abc ();
efg sc = new efg ();
fc.func ();
sc.func ();
Console.ReadKey ();
}
}
When the above code is compiled and executed, it produces the following results:
Inside first_space
Inside second_space
Nested namespace
Namespaces can be nested, that is, you can define another namespace within one namespace, as follows:
namespace namespace_name1
{
// code declaration
namespace namespace_name2
{
// code declaration
}
}
You can use the dot (.) Operator to access the members of the nested namespace as follows:
using System;
using first_space;
using first_space.second_space;
namespace first_space
{
class abc
{
public void func ()
{
Console.WriteLine ("Inside first_space");
}
}
namespace second_space
{
class efg
{
public void func ()
{
Console.WriteLine ("Inside second_space");
}
}
}
}
class TestClass
{
static void Main (string [] args)
{
abc fc = new abc ();
efg sc = new efg ();
fc.func ();
sc.func ();
Console.ReadKey ();
}
}
When the above code is compiled and executed, it produces the following results:
Inside first_space
Inside second_space
The above is [c # tutorial] C # Namespace (Namespace) content, more related content please pay attention to topic.alibabacloud.com (www.php.cn)!