10

Does it matter how many using compiler directives are in my classes? Is there a performance gain in removing those that aren't necessary?

Although I enjoy writing streamlined code, on occasion, code segments get modified, and don't have the opportunity to go back and check to see if all of the included namespaces are really necessary. Or, I don't go back and remove those that are auto-inserted by visual studio.

i.e.:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

Thanks!

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Hoppe
  • 6,508
  • 17
  • 60
  • 114
  • There is a tiny (probably immeasurable) performance gain when compiling the code in VS. And there can be some small “performance gain” when programmer is looking at that code. – svick Aug 27 '11 at 22:45
  • 1
    For the sake of your own performance in those times mentioned and in the name of easier maintenance, right-click in the VS editor, then Organise usings -> Remove and Sort. – Grant Thomas Aug 27 '11 at 22:45
  • There are plenty of Visual Studio add-ins that like to take advantage of your worrying. Including VS btw, not quite sure at what version the context menu item "Organize Using" became available. VS2010 has it. Seeing an actual perf improvement in compiling the code is something that somebody needs to report some day. Opening another file takes 50 milliseconds, a number that's been constant in Windows for a long time. The compiler is otherwise smart enough to remove the .assembly directive from the generated IL code when an assembly doesn't actually get used. – Hans Passant Aug 27 '11 at 23:32
  • Possible duplicate of [Why should you remove unnecessary C# using directives?](http://stackoverflow.com/questions/136278/why-should-you-remove-unnecessary-c-sharp-using-directives) – azam Mar 07 '17 at 02:10

1 Answers1

25

No, there's no performance advantage.

The compiler doesn't generate IL (executable code) for using statements. IL is only generated for those classes, method calls, etc. that take advantage of the using statements you provide.

Consequently, the only effect unused using statements might have is to slightly increase your build time, or make the next developer after you wonder why they are there.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501