[英]Why remove unused using directives in C#?

I'm wondering if there are any reasons (apart from tidying up source code) why developers use the "Remove Unused Usings" feature in Visual Studio 2008?

我想知道除了整理源代碼之外,為什么開發人員在Visual Studio 2008中使用“移除未使用的”特性?

10 个解决方案



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 compilation is slower.
  • 代碼編譯是慢。

On the other hand, there aren't many reasons to leave them in. I suppose you save yourself the effort of having to delete them. But if you're that lazy, you've got bigger problems!




I would say quite the contrary - it's extremely helpful to remove unneeded, unnecessary using statements.


Imagine you have to go back to your code in 3, 6, 9 months - or someone else has to take over your code and maintain it.


If you have a huge long laundry list of using statement that aren't really needed, looking at the code could be quite confusing. Why is that using in there, if nothing is used from that namespace??


I guess in terms of long-term maintainability in a professional environment, I'd strongly suggest to keep your code as clean as possible - and that includes dumping unnecessary stuff from it. Less clutter equals less confusion and thus higher maintainability.






This seems to me to be a very sensible question, which is being treated in quite a flippant way by the people responding.


I'd say that any change to source code needs to be justified. These changes can have hidden costs, and the person posing the question wanted to be made aware of this. They didn't ask to be called "lazy", as one person inimated.


I have just started using Resharper, and it is starting to give warnings and style hints on the project I am responsible for. Amongst them is the removal of redundant using directive, but also redundant qualifiers, capitalisation and many more. My gut instinct is to tidy the code and resolve all hints, but my business head warns me against unjustified changes.


We use an automated build process, and therefore any change to our SVN repository would generate changes that we couldn't link to projects/bugs/issues, and would trigger automated builds and releases which delivered no functional change to previous versions.

我們使用一個自動構建過程,因此對SVN存儲庫的任何更改都將生成無法鏈接到項目/bug /問題的更改,並將觸發自動構建和發布,對以前的版本沒有任何功能更改。

If we look at the removal of redundant qualifiers, this could possibly cause confusion to developers as classes our Domain and Data layers are only differentiated by the qualifiers.


If I look at the proper use of capitalisation of anachronyms (i.e. ABCD -> Abcd) then I have to take into account that Resharper doesn't refactor any of the Xml files we use that reference class names.

如果我考慮使用anachronyms(即ABCD -> ABCD)的恰當使用,那么我必須考慮到Resharper不會重構任何我們使用的引用類名稱的Xml文件。

So, following these hints is not as straight-forward as it appears, and should be treated with respect.




Less options in the Intellisense popup (particularly if the namespaces contain lots of Extension methods).


Theoretically Intellisense should be faster too.




In addition to the reasons already given, it prevents unnecessary naming conflicts. Consider this file:


using System.IO;
using System.Windows.Shapes;

namespace LicenseTester
    public static class Example
        private static string temporaryPath = Path.GetTempFileName();

This code doesn't compile because both the namespaces System.IO and System.Windows.Shapes each contain a class called Path. We could fix it by using the full class path,


        private static string temporaryPath = System.IO.Path.GetTempFileName();

or we could simply remove the line using System.Windows.Shapes;.




Remove them. Less code to look at and wonder about saves time and confusion. I wish more people would KEEP THINGS SIMPLE, NEAT and TIDY. It's like having dirty shirts and pants in your room. It's ugly and you have to wonder why it's there.




It also helps prevent false circular dependencies, assuming you are also able to remove some dll/project references from your project after removing the unused usings.




Code compiles quicker.




At least in theory, if you were given a C# .cs file (or any single program source code file), you should be able to look at the code and create an environment that simulates everything it needs. With some compiling/parsing technique you may even create a tool to do it automatically. If this is done by you at least in mind, you can ensure you understand everything that code file says.

至少在理論上,如果您得到一個c# .cs文件(或任何一個程序源代碼文件),您應該能夠查看代碼並創建一個環境來模擬它需要的一切。使用一些編譯/解析技術,您甚至可以創建一個工具來自動執行。如果這是您至少在頭腦中完成的,您可以確保您理解代碼文件所述的所有內容。

Now consider, if you were given a .cs file with 1000 using directives which only 10 was actually used. Whenever you look at a symbol that is newly introduced in the code that references the outside world, you will have to go through those 1000 lines to figure out what it is. This is obviously slows down the above procedure. So if you can reduce them to 10, it will help!


In my opinion, the C# using directive is very very weak, since you cannot specify single generic symbol without genericity being lost, and you cannot use using alias directive to use extension methods. This is not the case in other languages like Java, Python and Haskell, in those languages you are able to specify (almost) exactly what you want from the outside world. But event then, I will suggest to use using alias whenever possible.




Recently I got another reason why deleting unused imports is quite helpful and important.


Imagine you have two assemblies, where one references the other (for now let´s call the first one A and the referenced B). Now when you have code in A that depends on B everything is fine. However at some stage in your development-process you notice that you actually don´t need that code any more but you leave the using-statement where it was. Now you not only have a meaningless using-directive but also an assembly-reference to B which is not used anywhere but in the obsolete directive. This firstly increases the amount of time needed for compiling A, as B has to be loaded also.


So this is not only an issue on cleaner and easier to read code but also on maintaining assembly-references in production-code where not all of those referenced assemblies even exist.


Finally in our exapmle we had to ship B and A together, although B is not used anywhere in A but in the using-section. This will massively affect the runtime-performance of A when loading the assembly.




粤ICP备14056181号  © 2014-2020 ITdaan.com