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:47

The 'using' statement does not affect performance as it is merely a helper in qualifying the names of your identifiers. So instead of having to type, System.IO.Path.Combine(...), you can simply type, Path.Combine(...) if you have using System.IO.

查看更多
看风景的人
3楼-- · 2019-01-01 15:49

There's no IL construct that corresponds to using. Thus, the using statements do not increase your application memory, as there is no code or data that is generated for it.

Using is used at compile time only for the purposes of resolving short type names to fully qualified type names. Thus, the only negative effect unnecessary using can have is slowing the compile time a little bit and taking a bit more memory during compilation. I wouldn't be worried about that though.

Thus, the only real negative effect of having using statements you don't need is on intellisense, as the list of potential matches for completion while you type increases.

查看更多
旧时光的记忆
4楼-- · 2019-01-01 15:51

It won't change anything when your program runs. Everything that's needed is loaded on demand. So even if you have that using statement, unless you actually use a type in that namespace / assembly, the assembly that using statement is correlated to won't be loaded.

Mainly, it's just to clean up for personal preference.

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

Leaving extra using directives is fine. There is a little value in removing them, but not much. For example, it makes my IntelliSense completion lists shorter, and therefore easier to navigate.

The compiled assemblies are not affected by extraneous using directives.

Sometimes I put them inside a #region, and leave it collapsed; this makes viewing the file a little cleaner. IMO, this is one of the few good uses of #region.

查看更多
冷夜・残月
6楼-- · 2019-01-01 15:55

Code cleanliness is important.

One starts to get the feeling that the code may be unmaintained and on the browfield path when one sees superfluous usings. In essence, when I see some unused using statements, a little yellow flag goes up in the back of my brain telling me to "proceed with caution." And reading production code should never give you that feeling.

So clean up your usings. Don't be sloppy. Inspire confidence. Make your code pretty. Give another dev that warm-fuzzy feeling.

查看更多
回忆,回不去的记忆
7楼-- · 2019-01-01 15:57

You may have name clashes if you call your classes like the (unused) classes in the namespace. In the case of System.Text, you'll have a problem if you define a class named "Encoder".

Anyways this is usually a minor problem, and detected by the compiler.

查看更多
登录 后发表回答