What are the benefits of maintaining a “clean” list of using directives in C#?

穿精又带淫゛_ 提交于 2019-11-30 17:41:34

For me it's basically all about less noise (plus making Resharper happy!).

I would believe any improvement in compilation time would be minimal.

If you always only have the using directives that you need, and always have them appropriately sorted, then when you come to diff two versions of the code, you'll never see irrelevant changes.

Furthermore, if you have a neat set of using directives then anyone looking at the code to start with can get a rough idea of what's going to be used just by look at the using directives.

There's no runtime impact. It's purely compile time. It potentially impacts the following:

  1. Less chance for Namespace collisions
  2. Less "noise" in the code file
  3. Very explicit about which namespaces and possible types to expect in the file
  4. Using the menu to remove unused and Sort means more consistency with using statements among the devs. Less chance of dumb checkins just to fix it up.
  1. Less noise.
  2. Clear expectation of what types are used ("My UI layer depends upon System.Net. Wow, why?")
  3. Cleaner references: if you have the minimal set of using statements, you can cleanup your references. Often I see developers just keep throwing references into their projects, but they never remove them when they're no longer needed. If you don't have anything that actually needs a reference (and a using statement counts), it becomes trivial to clean up your references. (Why would you want to do that? In large systems that have been decomposed into components it will streamline your build dependencies by eliminating the unused deps.)

For me, a clean list of using statements at the beginning can give a good understanding of the types to expect.

I saw a decent gain in compile time a few years ago when I first installed ReSharper (on an 18 project solution). Since then its just been about keeping it clean.

I can't speak to the benefits in compile time and performance, but there's a lower chance of namespace collisions if you have minimize your using declarations. This is especially important if you are using more than one third party library.

There is one compile-time difference: when you remove a reference, but still have a using directive in your code, then you get a compiler error. So having a clean list of using directives makes it a little bit easier to remove unused references.

Usually the compiler removes unused references, but I don't know if that works when a using is in the code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!