Why should you remove unnecessary C# using directi

2019-01-01 15:31发布

For example, I rarely need:

using System.Text;

but it's always there by default. I assume the application will use more memory if your code contains unnecessary using directives. But is there anything else I should be aware of?

Also, does it make any difference whatsoever if the same using directive is used in only one file vs. most/all files?


Edit: Note that this question is not about the unrelated concept called a using statement, designed to help one manage resources by ensuring that when an object goes out of scope, its IDisposable.Dispose method is called. See Uses of "using" in C#.

14条回答
浮光初槿花落
2楼-- · 2019-01-01 15:34

They are just used as a shortcut. For example, you'd have to write: System.Int32 each time if you did not have a using System; on top.

Removing unused ones just makes your code look cleaner.

查看更多
无与为乐者.
3楼-- · 2019-01-01 15:36

Your application will not use more memory. Its for the compiler to find classes you use in the code files. It really doesnt hurt beyond not being clean.

查看更多
零度萤火
4楼-- · 2019-01-01 15:36

if you want to maintain your code clean, not used using statements should be removed from the file. the benefits appears very clear when you work in a collaborative team that need to understand your code, think all your code must be maintained, less code = less work, the benefits are long term.

查看更多
千与千寻千般痛.
5楼-- · 2019-01-01 15:39

Do not forget that the compiler do a lot of work to optimize everything when building your project. Using that is used in a lot of place or 1 shouldn't do a different once compiled.

查看更多
千与千寻千般痛.
6楼-- · 2019-01-01 15:44

There are few reasons for removing unused using(s)/namespaces, besides coding preference:

  • removing the unused using clauses in a project, can make the compilation faster because the compiler has fewer namespaces to look-up types to resolve. (this is especially true for C# 3.0 because of extension methods, where the compiler must search all namespaces for extension methods for possible better matches, generic type inference and lambda expressions involving generic types)
  • can potentially help to avoid name collision in future builds when new types are added to the unused namespaces that have the same name as some types in the used namespaces.
  • will reduce the number of items in the editor auto completion list when coding, posibly leading to faster typing (in C# 3.0 this can also reduce the list of extension methods shown)

What removing the unused namespaces won't do:

  • alter in any way the output of the compiler.
  • alter in any way the execution of the compiled program (faster loading, or better performance).

The resulting assembly is the same with or without unused using(s) removed.

查看更多
何处买醉
7楼-- · 2019-01-01 15:45

The using statement just keeps you from qualifying the types you use. I personally like to clean them up. Really it depends on how a loc metric is used

查看更多
登录 后发表回答