views:

22

answers:

2

Hello everybody

I am sure that codes will produce same result but CLR will consider unused namespace directives while allocating memory ? Another question can be that CLR consider unused code blocks while allocating memory ?

+2  A: 

It will not make a difference at runtime whether you have unused using directives in your code or not.

The using directive is there for convenience so that you can write

using System.IO;
[...]
string path = Path.GetDirectoryName(filename);

instead of writing the fully qualified name

string path = System.IO.Path.GetDirectoryName(filename);

every time that you want to use a type from the System.IO namespace. The directive tells the compiler which namespaces to search for the types that are used within a file. The compiler will then actually replace the first example with the second, i.e. the IL code in the assembly will always use fully qualified type names. Unused namespaces will not appear in the compiled assembly.

However, there are reasons to keep a clean list of the namespaces that get imported. John Feminella stated in a related question:

There are a few reasons you'd want to take them out.

  • It's pointless. They add no value.
  • It's confusing. What is being used from that namespace?
  • If you don't, then you'll gradually accumulate pointless using statements as your code changes over time.
  • Static analysis is slower.
  • Code compiles quicker.
0xA3
+1 My reason for dropping them: they're untidy
Tim Robinson
@0xA3 - What about namespace which directed by unused code block?
Freshblood
A: 

To add to 0xA3's answer: using directives exist only in the C# source code. Like comments, they don't exist in the assembly binary.

Tim Robinson