简体   繁体   中英

c# using namespace statement ordering

I thought I read somewhere that ordering your using statements and getting rid of unused ones had some kind of performance benefit...but I can't seem to find any evidence or resources to support that...is there any truth to this?

No, the using statement, for setting the namespaces, has no performance cost. The IL code produced will be the same, regardless of the order of the statements. The only advantage would be for clarity and readability. Also, removing unused ones would speed up compilation time, but run time performance won't be any different.

From the msdn ,

using-namespace-directives in the same compilation unit or namespace body do not affect each other and can be written in any order.

Very much useful link you would like to visit.

An additional issue not mentioned by other commenters: Your using statements affect what appears in Intellisense (particular for extension methods). Removing unused statements will keep your Intellisense performing better and only showing relevant extension methods.

There would not be. At least certainly not at runtime. It is possible that at build time it takes some extra time to process them, but it wouldn't be noticable. The using statements aren't carried over into the IL code, everything has its full name instead, so it is only a build-time thing.

Now, personally, I hot-key "sort and remove usings" and hit it constantly, just because it keeps the code cleaner. But it is just an obsessive-compulsive thing:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM