0

I'm just curious if unnecessary USINGs in my code slows down compiling of Visual Studio solution. Over time there are new and new usings in my code because of changes in code when some features are added, reimplemented and sometimes removed.

Should I even care about the number of USINGs? How can I remove unnecessary USINGs?

Thanks for answers!

(If this question is supposed to be a community wiki question, please let me know in comments and I'll change it)

MartyIX
  • 27,828
  • 29
  • 136
  • 207
  • 1
    possible duplicate of [overhead to unused "using" declarations ?](http://stackoverflow.com/questions/641234/overhead-to-unused-using-declarations) – ChrisF Jul 27 '10 at 20:20
  • How big is the performance decrease? "None at all". – ChrisF Jul 27 '10 at 20:21
  • 1
    possible dupe of: http://stackoverflow.com/questions/136278/why-should-you-remove-unnecessary-c-using-directives – The Mirage Jul 27 '10 at 20:26

5 Answers5

2

If you are using VS2008/2010: Just right click on the usings and select "Organize Usings" -> "Remove and Sort" from the context menu. Compiling might become a little bit faster but I guess in most situations it is not noticable.

blueling
  • 2,003
  • 1
  • 17
  • 16
  • Thanks! That's a useful function! – MartyIX Jul 27 '10 at 20:43
  • I bumped on this : how to organize usings across your entire solution - http://blogs.msdn.com/b/djpark/archive/2008/08/16/organize-usings-across-your-entire-solution.aspx – Larry Apr 15 '11 at 06:43
1

using statements are translated into "try finally"s, so you should not see any performance problems.

  • 2
    I believe he is talking about the other kind of usings. – Brian Rasmussen Jul 27 '10 at 20:21
  • Different `using` statement I believe. You seem to be refering to something like using(MemoryStream ms = ...) and the OP seems to be refering to using System.Data.SqlClient (or other such). – AllenG Jul 27 '10 at 20:21
  • I'm sorry I didn't make it clear but my question is about "USING System;" usings. – MartyIX Jul 27 '10 at 20:23
1

They have no bearing on the performance of your application. Funny cause I just investigated this myself the other day. ScottGu and Hanselman both posted about this in the past. Links coming in a few.

Having less usings may speed up compile time, especially in C# 3.0, but won't have any performance impact. Also, excessive using statements will clutter up Intellisense and ReSharper.

The Mirage
  • 378
  • 2
  • 11
1

Only a speed up in compile time, no change in performance. VS has this feature called Organize Usings with a remove and sort option which removes unused usings and organizes the rest. Mostly for clean code I would use this. Compile time shouldnt show that big of a difference anyway unless you have WAY too many usings.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
1

Perhaps you suspect the using statement behaves similar to #include in C or C++ - the latter may have a noteable slowdown of compilation speed. That is not the case. A using statement in C# is more or less just a syntactical shortcut, so you don't have to write 20 times

System.Collections.Generic.List<T>

when you need a generic list 20 times in your class. Not the

using System.Collections.Generic;

at the beginning of your class file causes the C# compiler having to compile some List code, it is only the

var x = new List<int>();

or (if you prefer)

var x = new System.Collections.Generic.List<int>();

later in your code that makes a noteable difference. I wrote noteable, because in fact having some unneeded using the compiler will have to do some additional namespace lookup, but in most practical cases you won't notice any difference,

Doc Brown
  • 19,739
  • 7
  • 52
  • 88