C# namespace (Namespace)

高洛峰
Release: 2017-02-08 13:25:59
Original
1405 people have browsed it

Define namespace

The definition of a namespace starts 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 the namespace version, the name of the namespace will be placed in front, as follows:

namespace_name.item_name;

The following program demonstrates naming Usage of space:

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 will produce the following results:

Inside first_space Inside second_space

using keyword

The using keyword indicates that the program is using names in the given namespace. For example, we use the System namespace in our 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") there");

You can also use the using namespace directive, so that you don’t need to add the namespace name in front of it when using it. This directive tells the compiler that subsequent code uses names from the specified namespace. The following code demonstrates the use of namespaces.

Let us rewrite the above example using the using specification:

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 will generate The following results:

Inside first_space Inside second_space

Nested namespace

Namespaces can be nested, i.e. you can define one namespace inside another namespace, As shown below:

namespace namespace_name1 { // Code declaration namespace namespace_name2 { // Code declaration }}

You can use the dot (.) operator to access members of a 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 ef g                                                                                                                                                                  Console.WriteLine("Inside second_space"); } } } } class TestClass{ static voidMain(string[] args) { abc fc = new abc(); efg sc = new efg(); sc. func(); Console.ReadKey(); }}

When the above code is compiled and executed, it will produce the following results:

Inside first_space Inside second_space

For more C# namespace (Namespace) related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template