Right, I’ve usually used ‘using’ directives as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AwesomeLib
{
//awesome award winning class declarations making use of Linq
}
i’ve recently seen examples of such as
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AwesomeLib
{
//awesome award winning class declarations making use of Linq
namespace DataLibrary
{
using System.Data;
//Data access layers and whatnot
}
}
Granted, i understand that i can put USING inside of my namespace declaration. Such a thing makes sense to me if your namespaces are in the same root (they organized).
System;
namespace 1 {}
namespace 2
{
System.data;
}
But what of nested namespaces? Personally, I would leave all USING declarations at the top where you can find them easily. Instead, it looks like they’re being spread all over the source file.
Is there benefit to the USING directives being used this way in nested namespaces? Such as memory management or the JIT compiler?
IL Code does Not reflect C#
usingRuntime Performance
Because you’re asking about runtime performance, here’s a look into what’s happening underneath the source code.
If you look at the compiled IL code with Microsoft’s IL Diassembler tool (as we’re doing here) you will see all class names are fully qualified all the time no matter how the programmer used
usingin the source code.In the following sample of compiled IL code notice no “shortcut” mechanism is seen although
usingwas in the original C# source code files. For example IL describes one longextends [System.Web]System.Web.UI.Pagewhereas C# would have used: Pageand alsousing System.Web.UI;(two separate statements).In the compiled IL all classes are fully qualified regardless.
This means there are no performance benefits or drawbacks at runtime based on the design time
usingstatements.Compile Time
Depending on how you scatter about your
usings andnamespaces in the source code, there might be more or less of the keywords hanging around. The compiler has to see them all and process them all but overall the compile performance would be negligible for something this trivial, compared to all things a compiler has to do to make the finished product.Design Time Benefits
namespaces are an organizational technique and
usingis a way of managing them at the source code level (and to instruct the compiler how you’re using them so it can compile the program accordingly). When C# source specifiesusing System.Web.UI;, nothing is imported and the file size doesn’t grow larger (because the assembly is already referenced in); insteadusingis simply effecting a shorter syntax to the contents of that namespace, from within the scope theusingis used in, whether that be in the entire file scope or a declared namespace scope inside the file.The benefit to the programmer is reduction of ambiguous class name conflicts between multiple namespace
usings if they are judiciously used.Organization of source code namespaces is represented differently in the compiled IL code (as seen in the above example).