开发者

Unused namespace directives can cause any change?

开发者 https://www.devze.com 2023-01-09 01:30 出处:网络
I am sure that codes will produce same result but CLR will consider unused nam开发者_如何学Cespace directives while allocating memory ? Another question can be that CLR consider unused code blocks whi

I am sure that codes will produce same result but CLR will consider unused nam开发者_如何学Cespace directives while allocating memory ? Another question can be that CLR consider unused code blocks while allocating memory ?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消