简体   繁体   English

命名空间和子命名空间以及它们在C#中引用的库

[英]Namespaces and Sub-Namespaces and the libraries they reference in C#

Can I have 我能有......吗

namespace somenamespace
{
//references functionality in DLL_1
      namespace somesubnamespace
      {
       //references functionality in DLL_2
      }
}

And if I do this, when I use just somenamespace, will it only include DLL_1 in the solution and if I use somenamespace.somesubnamespace will it include DLL_1 and DLL_2 in the solution? 如果我这样做,当我仅使用somenamespace时,解决方案中是否仅包含DLL_1;如果我使用somenamespace.somesubnamespace,则解决方案中是否包含DLL_1和DLL_2?

Or are DLL_1 and DLL_2 set on the project that contains the namespace and no matter which I use, both DLLs will be copied? 还是在包含名称空间的项目中设置了DLL_1和DLL_2,无论我使用哪个名称,两个DLL都会被复制?

You only need to add references to the DLLs that your code actually uses (directly or indirectly). 您只需要添加对代码实际使用的DLL的引用(直接或间接)。

Therefore, if library X has two classes that reference library A and B respectively, and you only use the class that references library A, you do not need library B at all. 因此,如果库X有两个分别引用库A和B的类,而您仅使用了引用库A的类,则根本不需要库B。

Namespaces are organizational concepts and have nothing to do with it. 命名空间是组织概念,与命名空间无关。

If you add references to the DLLs, they will be copied. 如果您添加对DLL的引用,它们将被复制。 Since you must add the reference in order to use them, both will always be copied. 由于必须添加引用才能使用它们,因此两者都将始终被复制。

Your code above is exactly equivalent to: 您上面的代码完全等同于:

namespace.somesubnamespace
{

}

If you write it your way, or if you write it this way, accessing somesubnamespace is done exactly the same way in your code. 如果您以自己的方式编写代码,或者以这种方式编写代码,则访问somesubnamespace空间的方式与您的代码完全相同。

Namespaces are essentially just labels to organize your code. 命名空间实质上只是用于组织代码的标签。 Depending on what your code is accessing will dictate what references you need - but the answer to your question is "no", the references for namespace will not necessarily be required. 取决于您的代码访问的内容将决定您需要哪些引用-但是对您的问题的回答是“否”,则不一定需要namespace的引用。 namespace.somesubnamespace is an independent namespace in that regard. 就此而言, namespace.somesubnamespace是一个独立的命名空间。

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

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