C#编程练习:名字空间

新建一个工程后,在解决方案资源管理器中新建项,如图所示。

选择类,输入类文件名,选择‘创建’项

一、

在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();
        }

运行程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值