1

I was under review of team code. I observe they had used class ClassAby writing using AAA.BBB; at the upper portion of the class; also they have sometime used class ClassB by AAA.BBB.ClassB. There are two basic questions.

  1. Is there any performance issue while using above scenario. What is recommended
  2. When I declare namespace; are all classes get loaded of that namespace or not.

Please assist here. Thanks.

Rajaram Shelar
  • 7,537
  • 24
  • 66
  • 107

3 Answers3

2

I'll answer these as best as I can, I don't have any sources on hand, just experience (maybe someone can help with that).

  1. There is no performance issue with importing a namespace versus calling it directly. When the compiler runs through it, you can think of it as always being fully qualified in the end. The using statement for namespaces is more to assist the developer so they don't have to fully qualify it each time. In fact, for your ClassB example, it could be that there is a collision with multiple namespaces defining the same class name. For example, the Calendar class is both in System.Globalization and System.Web.UI, so you have to fully qualify one or the other when using them.

  2. Generally, all code is compiled into an assembly by the project it's under. Referencing any code inside of the assembly will load all of the associated code. Note, however that the code isn't necessarily compiled for use by the JIT until it's actually called.

Joshua
  • 8,112
  • 3
  • 35
  • 40
  • Means not all classes in the namespace using AAA.BBB would load. It will load only ClassA and ClassB. Is it correct? – Rajaram Shelar Oct 10 '13 at 04:10
  • 2
    Unfortunately, not quite. A namespace can be used and added anywhere. If `ClassA` and `ClassB` are in the same *assembly* (meaning output as a .dll or .exe) then they will load at the same time. If `ClassA` is in another assembly, then they can load at separate times. Generally, with .Net development, an assembly is defined at the project level, so if the classes are under the same project, then they compile into the same assembly. – Joshua Oct 10 '13 at 04:14
1

Namespaces are for organizing your code (while preventing name collisions). They have no bearing on performance at all.

Matthew
  • 24,703
  • 9
  • 76
  • 110
  • "They have no bearing on performance at all." --- the "performance" is a buzz word. Having a lot of `using` may make the compilation slower. Thus make compilation performance worse. – zerkms Oct 10 '13 at 04:03
  • 2
    @zerkms Do you know of any evidence to support that it makes any tangible difference in compilation times? – Matthew Oct 10 '13 at 04:04
  • 1
    I agree with @Matthew, The compilation argument is true for C++ `#include`'s but not for C# `using`'s. – Scott Chamberlain Oct 10 '13 at 04:05
  • @Matthew: "tangible" --- the question by itself is not practical but theoretical. Theoretically - it makes it slower. So theoretically it has to do with compilation performance. – zerkms Oct 10 '13 at 04:06
1

Fully qualified names in code are distracting and noisy. I prefer to never have them. If there is a namespace conflict, a using alias can resolve that.

only for places where there is a conflict with there being two Class defined in different namespaces, and, even then, I'll still rather use a using to differentiate them:

using MyClassB=AAA.BBB.ClassB;
//  :
var myClassB= new MyNS();

in terms of performance, you can see the answer here:

The using directive is only syntactic sugar that disappears during compilation. Whether or not the namespace was included via using or mentioned in a fully-qualified type name is totally irrelevant in the resulting bytecode. Hence, there is no performance benefit at runtime by using one or the other.

Community
  • 1
  • 1
Afshin
  • 1,222
  • 11
  • 29