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

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() 方法。");
}
}
}

输出:

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





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