Question:
c# (general .net) question about practical use of namespaces?
a
2010-08-06 06:33:42 UTC
hard to explain what i mean in the title alone. we wrote a huge application and have several static classes that we use for extension methods and general utility methods. we use these files' namespaces in pretty much every single .cs file in the solution even if we don't use any of the methods contained in the classes.

so my question is this: what are the implications of using namespaces even when you are not using any of the members contained within them? is it a memory or performance issue? if we use a namespace that contains a few hundred extension methods and never use any of them, how does this impact the application?
Four answers:
anonymous
2010-08-06 07:02:04 UTC
The compiler optimizes the calls whether you use the namespace or not. However, if the files in the namespace are compiled into a separate DLL, and you use any of those methods, it will still have to link and include the DLL, but there is negligible performance implications of this.



Namespaces are just a greater form of encapsulation. The true power of namespaces comes when you have private or protected classes that the namespace can make use of that aren't exposed beyond the namespace level to the external API. I think of it as "like a class of classes". If all you are doing with namespaces is static classes for utility methods, then no there is little point or benefit.
anonymous
2010-08-06 14:55:15 UTC
it doesn't, namespaces exist to separate like named methods and or properties, used more in OOP so if u wanna incorporate an existing class into a project method names etc dont clash. If ur talking about practicality it would act as an index so u may see a few xtra hundred kb or so but nothing that would seriously undermine the usability of the app.

If u mean u dont use any of the accessor methods listed in the static class an reference the static class anyway i don't think anything would happen.
Pfo
2010-08-06 15:20:01 UTC
The practical use of namespaces is so that you could have System.Windows.Forms.Textbox and Mynamespace.Textbox, where two classes are named Textbox, but you don't get compilation errors claiming that two classes have the same name. That's about it. Organizing classes into namespaces does bring performance concerns, it might be better to use more namespaces when you have many classes, but we're talking on the order of hundreds and thousands of classes before the performance becomes noticeable. I personally like to use namespaces just to organize code into groups, generally use of namespaces is to resolve class name collisions when using multiple classes with the same name.
Bonkers!
2010-08-06 13:50:21 UTC
I could be wrong, but my understanding of namespaces is merely to help the "compiler" find all the pieces you're referecing. If add a "using" statement, but then never actually reference anything in that namespace, I don't think it's even included in the build.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...