简体   繁体   中英

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?

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?

You only need to add references to the DLLs that your code actually uses (directly or indirectly).

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.

Namespaces are organizational concepts and have nothing to do with it.

If you add references to the DLLs, they will be copied. 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.

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.somesubnamespace is an independent namespace in that regard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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