zoukankan      html  css  js  c++  java
  • C#编程(七)----------命名空间

    命名空间

    命名空间的设计目的是为了提供一种让一组名称与其他名称分隔开的方式。在一个命名空间中声明的类的名称与另一个命名空间中声明的相同的类的名称不冲突。

    关键字:namespace

    namespace namespace_name

    {   // 代码声明

    }

    请看下面的例子:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace ConsoleApplication12

    {

        class Program

        {

            

            static void Main(string[] args)

            {

                First.T1 t1 = new First.T1();

                Second.T2 t2 = new Second.T2();

                t1.func();

                t2.func();

                Console.ReadKey();

            }

        }

    }

    namespace First

    {

        class T1

        {

            public void func()

            {

                Console.WriteLine("t1");

            }

        }

    }

    namespace Second

    {

        class T2

        {

            public void func()

            {

                Console.WriteLine("t2");

            }

        }

    }

    在使用类T1的时候前面加上了First命名空间,其实在使用Console的时候,完整是写法应该是System.Console.ReadKey();

    因为使用了using System;所以可以省略这个命名空间.

    using关键字

    using 关键字表明程序使用的是给定命名空间中的名称。用法前面已经给出,省略前面的命名空间的写法.

    也可以使用 using 命名空间指令,这样在使用的时候就不用在前面加上命名空间名称。该指令告诉编译器随后的代码使用了指定命名空间中的名称。

    using System;

    using First;

    using Second;

    namespace ConsoleApplication12

    {

        class Program

        {

            

            static void Main(string[] args)

            {

                T1 t1 = new First.T1();

                T2 t2 = new Second.T2();

                t1.func();

                t2.func();            

                System.Console.ReadKey();

            }

        }

    }

    namespace First

    {

        class T1

        {

            public void func()

            {

                Console.WriteLine("t1");

            }

        }

    }

    namespace Second

    {

        class T2

        {

            public void func()

            {

                Console.WriteLine("t2");

            }

        }

    }

    嵌套命名空间

    命名空间可以被嵌套,即在一个命名空间里嵌套另外一个命名空间.

    namespace First

    {

        //代码

        namespace Second

        {

            //代码

        }

    }

    使用(.)运算符访问嵌套的命名空间的成员.例如:

    using System;

    using First;

    using First.Second;

    namespace ConsoleApplication12

    {

        class Program

        {

            

            static void Main(string[] args)

            {

                T1 t1 = new T1();

                T2 t2 = new T2();

                t1.func();

                t2.func();            

                System.Console.ReadKey();

            }

        }

    }

    namespace First

    {

        class T1

        {

            public void func()

            {

                Console.WriteLine("t1");

            }

        }

        namespace Second

        {

            class T2

            {

                public void func()

                {

                    Console.WriteLine("t2");

                }

            }

        }

    }

    因为using语句在C#文件的开头,而C和C++也罢#include语句放在语句的开头,所以从C++迁移到C#的程序员唱吧命名空间与C++风格的头文件相混淆.using语句在这些文件之间并没有建立物理连接,C#也没有对应于C++头文件的部分.

    名称空间的别名

    using为命名空间创建别名的用法规则为:

    using alias=namespace|type

    其中namespace表示创建命名空间的别名;而type表示创建类型别名。例如在.NET Office应用中,常常会引入Microsoft.Office.Interop.Word.dll程序集,在引入命名空间时为了避免繁琐的类型输入,我们通常为其创建别名如下:

    using MSWord=Microsoft.Office.Interop.Word;

    这样,就可以在程序中以MSWord来代替Microsoft.Office.Interop.Word前缀,如果要创建Application对象,则可以是这样,

    private static MSWord.Application ooo=new MSWord.Application();

    同样,也可以创建类型的别名,用法为:

    using MyControle=System.Console;class UsingEx{   public static void Main()   {    MyConsole.WriteLine("应用了类的别名");    }}

    而创建别名的另一个重要的原因在于同一cs文件中引入的不同命名空间中包括了相同名称的类型,为了避免出现名称冲突可以通过设定别名来解决,例如:

    Namespace InsideDotNet.Keyword.Using{   using BoyPlayer=Boyspace.Player;   using Girlplayer=Girlspace.Player;       class UsingEx    {          public static void Main()          {               BoyPlayer.Play();               Girlplayer.Play();           }     }}namespace Boyspace{       public class Player       {             public static void Play()             {                    System.Console.WriteLine("Boys play football.");              }        }}namespace Girlspace{       public class Player       {            public static void Play()            {                  System.Console.WriteLine("Girls play violin.");             }        }}

     
  • 相关阅读:
    Windows Azure Web Site (19) Azure Web App链接到VSTS
    Windows Azure Virtual Machine (35) Azure VM通过Linked DB,执行SQL Job
    Azure PowerShell (16) 并行开关机Azure ARM VM
    Windows Azure Virtual Network (12) 虚拟网络之间点对点连接VNet Peering
    Azure ARM (21) Azure订阅的两种管理模式
    Windows Azure Platform Introduction (14) 申请海外的Windows Azure账户
    Azure ARM (20) 将非托管磁盘虚拟机(Unmanage Disk),迁移成托管磁盘虚拟机(Manage Disk)
    Azure ARM (19) 将传统的ASM VM迁移到ARM VM (2)
    Azure ARM (18) 将传统的ASM VM迁移到ARM VM (1)
    Azure Automation (6) 执行Azure SQL Job
  • 原文地址:https://www.cnblogs.com/android-blogs/p/6428889.html
Copyright © 2011-2022 走看看