What's the value in removing and/or sorting Usings?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 02:46:17

问题


I've always run Remove and Sort Usings as a matter of course, because it seems the right thing to do. But just now I got to wondering: Why do we do this?

Certainly, there's always a benefit to clean & compact code. And there must be some benefit if MS took the time to have it as a menu item in VS.

Can anyone answer: why do this? What are the compile-time or run-time (or other) benefits from removing and/or sorting usings?


回答1:


As @craig-w mentions, there's a very small compile time performance improvement.

The way that the compiler works, is when it encounters a type, it looks in the current namespace, and then starts searching each namespace with a using directive in the order presented until it finds the type it's looking for.

There's an excellent writeup on this in the book CLR Via C# by Jeffrey Richter (http://www.amazon.com/CLR-via-4th-Developer-Reference/dp/0735667454/ref=sr_1_1?ie=UTF8&qid=1417806042&sr=8-1&keywords=clr+via+c%23)

As to why MS provided the menu option, I would imagine that enough internal developers were asking for it for the same reasons that you mention: cleaner, more concise code.




回答2:


There's probably a teeny-tiny (i.e. minuscule/virtually unmeasurable) performance improvement during compilation because it doesn't have to search through namespaces that you aren't actually using for unqualified types. I do it because it's just neater and easier to read in the end. Also, I use the Productivity Power Tools and have them set to do the Remove and Sort when I save the file.



来源:https://stackoverflow.com/questions/27322762/whats-the-value-in-removing-and-or-sorting-usings

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