C# 命名空间

创建一个控制台应用程序,建立一个命名空间 N1,在命名空间 N1 中有一个类 A,在项目中使用 using 指令引入命名空间 N1,然后在命名空间 LianXi 中即可实例化命名空间 N1 中的类,此类中的 Myls 方法,代码如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using N1; // 引入命名空间,如果不引入
// 下面的 Mian 函数内的 A 类会报错报红。

namespace CSharp
{
class LianXi
{
static void Main(string[] args)
{
A oa = new A();
oa.Myls();
}
}
}


namespace N1
{
class A
{
public void Myls()
{
Console.WriteLine("这是命名空间 N1 中的 Myls() 方法。");
}
}
}

输出:

1
2
这是命名空间 N1 中的 Myls() 方法。
请按任意键继续. . .




笔记:
《C# 入门到精通》 P18。

感谢支持!