views:

46

answers:

5

Hi,

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)

+1  A: 

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

Sam
I believe he is talking about the other kind of usings.
Brian Rasmussen
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
I'm sorry I didn't make it clear but my question is about "USING System;" usings.
MartyIX
A: 

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
+1  A: 

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
Thanks! That's a useful function!
MartyIX
A: 

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
A: 

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
I knew it doesn't behave as C++ #includes. But still thanks for an answer!
MartyIX