C# using Directive
July 7, 2008
Scott Hanselman has a new post that caught my eye: Back to Basics - Do namespace using directives affect Assembly Loading? In that post he points to an interesting StyleCop rule that caused him to do a double-take: SA1200: Using Directives Must Be Placed Within Namespace.
According to the StyleCop post, there are three things to consider when deciding the placement of the using directive:
There are subtle differences between placing
usingdirective within a namespace element, rather than outside of the namespace, including:1. Placing using-alias directives within a namespace eliminates compiler confusion between conflicting types.
2. Placement of using directives can affect how and when the .Net Framework will load referenced assemblies.
3. When multiple namespaces are defined within a single file, placing using directives within the namespace elements scopes references and aliases.
According to Scott:
However, the second rule in the post said:
"However, placing the using statements [Ed. Note: They mean "directives"] within a namespace element allows the framework to lazy load the referenced assemblies at runtime. In some cases, if the referencing code is not actually executed, the framework can avoid having to load one or more of the referenced assemblies completely. This follows general best practice rule about lazy loading for performance."
This stopped me in my tracks. This rocks the very bedrock that my knowledge of the CLR stands on. I’m like, NO WAY, and then I oscillated back and forth between denial and acceptance. Then, I settled on denial. I don’t buy it. A using directive is for aliasing and is a kind of syntactic sugar. Ultimately the IL is the same. Assembly loading won’t be affected as the assembly manifest doesn’t change.
I have always put my using directive outside the namespace declarations. That was the way everyone did and to me it looked better. Every now and then I would see a code sample that placed the using directive inside the namespace declaration and it always made me wonder why you would do that.
Going to the MSDN Library, we find the following for the using Directive:
The using directive has two uses:
- Create an alias for a namespace (a using alias).
- Permit the use of types in a namespace, such that, you do not have to qualify the use of a type in that namespace (a using directive).
Granted, this page appears to only concern itself with how the directive is used, but you would think that if there were any possibility that position would affect the loading of classes that it would be called out in the Remarks section.
Scott was right to question the statement. He created quick test that seems to bear out the fact that position of the using directive does not affect the loading characteristics of the code. In addition, several people in the comments performed similar tests, including checking the generated IL code and came up with the same conclusion. He is waiting for some official feedback from someone on the compiler team.
So now I know that the only real reason to put the directive inside the namespace is to avoid conflicts, which I haven’t had so far.
Side note: Did you know that there is a difference between the using Directive and the using Statement? I never really paid attention or made the association that they were the same word but differentiated by context.
Popularity: 9% [?]
Comments
Got something to say?