新建一个工程后,在解决方案资源管理器中新建项,如图所示。
选择类,输入类文件名,选择‘创建’项
一、
在sunhello.cs中定义两个类,分别是两个say方法
internal class sunhello
{
public static void say()
{
Console.WriteLine("hello,world");
}
}
internal class jsonhello
{
public static void say()
{
Console.WriteLine("hello,world");
}
}
在Mian中调用两个类中的Say方法
namespace text_namespace
{
internal class Program
{
static void Main(string[] args)
{
sunhello.say();
jsonhello.say();
Console.ReadKey();
}
}
}
选中应用程序,设置为启动项目
执行程序,观察运行结果
二、
定义两个不同的名字空间,sun、json。在两个不同的名字空间中,定义一个类,类名都为hello
namespace sun
{
internal class hello
{
public static void say()
{
Console.WriteLine("hello,world");
}
}
}
namespace json
{
internal class hello
{
public static void say()
{
Console.WriteLine("hello,world");
}
}
}
重写Main方法,调用不同名字空间中类的方法
static void Main(string[] args)
{
//sunhello.say();
//jsonhello.say();
sun.hello.say(); //Sun名字空间中Hello类的Say方法
json.hello.say();//json名字空间中Hello类的Say方法
Console.ReadKey();
}
运行程序
三、
引用名字空间sun与json
using sun;//引用名字空间sun
using Myjson=json;//引用名字空间json,并且给名字空间取个别名
重写Main方法
static void Main(string[] args)
{
//sunhello.say();
//jsonhello.say();
hello.say(); //这里的sun可以省略
Myjson.hello.say();//引用别名,区分hello是哪个名字空间的类
Console.ReadKey();
}
运行程序