简体   繁体   English

C#中的命名空间

[英]Namespace in c#

namespace College
{
    namespace Lib
        {
            class Book
        {
            public void Issue()
            {
                // Implementation code
            }
        }
            class Journal
        {
            public void Issue()
            {
                // Implementation code
            }
        }
    }
}

Now to use Issue() method of class Book in a different namespace, the following two approaches work. 现在,要在不同的名称空间中使用Book类的Issue()方法,可以使用以下两种方法。

  1. College.Lib.Book b = new College.Lib.Book(); b.Issue();

  2. using College.Lib; Book b = new Book(); b.Issue();

And the following two approaches don't work. 并且以下两种方法不起作用。

i. 一世。 using College; Lib.Book b = new Lib.Book(); b.Issue();

ii. ii。 using College.Lib.Book; Book b = new Book(); b.Issue();

Why don't the last two codes work? 为什么后两个代码不起作用?

In the first case, the original designers of C# decided that a using directive should bring the types in a namespace into scope, so to speak, but not bring the namespaces in a given namespace into scope. 在第一种情况下,C#的原始设计者决定使用using指令应将名称空间中的类型带入范围,可以这么说,但不能将给定名称空间中的名称空间带入范围。 It was felt that "using" means "I have a bunch of types I want to use" and not "I have a bunch of sub-namespaces I want to use". 有人认为“使用”的意思是“我有一堆要使用的类型”,而不是“我有一堆要使用的子命名空间”。

In the second case: the feature of "using" a type was added to C# 6. It brings the static members of the type "into scope". 在第二种情况下:“使用”类型的功能已添加到C#6中。它将“类型”的静态成员带入范围。 Perhaps you are using an older version of C#? 也许您使用的是C#的旧版本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM