49

I've just installed resharper and it's letting me know the namespaces i'm not actually using in each of my classes.

which lead me to the question - is there actually any overhead in leaving these, unused, using declarations in?

is it just a matter of tight code, or is there a performance hit in invoking these namespaces when i don't need to?

nailitdown
  • 7,868
  • 11
  • 36
  • 37
  • i was surprised i couldn't find it too.. feel free to close if it's a dup. – nailitdown Mar 13 '09 at 02:29
  • Duplicate: "Why removing unused usings in C#?" => http://stackoverflow.com/questions/629667/why-removing-unused-usings-in-c – dance2die Mar 13 '09 at 02:29
  • @nailitdown: thank god, you added a "using" tag. A question below yours was the one I was looking for when I searched by "tag" – dance2die Mar 13 '09 at 02:30
  • no worries you can close it, i have my answer :) – nailitdown Mar 13 '09 at 02:30
  • I blame this all on SO search engine! ;) "Using" is not a good keyword to search for... – dance2die Mar 13 '09 at 02:33
  • use site:stackoverflow.com yoursearchtermhere On google. – George Stocker Mar 13 '09 at 02:42
  • How do you pursuade it to not stick in stuff you never asked for in the first place? (Grumble: typical VS - randomly throwing in code you never asked for and aren't ever going to use but will get blamed for if it breaks something ...) – dkretz Mar 13 '09 at 03:21
  • @le dorfier - can you amend the default templates in C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ .... ? – nailitdown Mar 13 '09 at 04:47
  • possible duplicate of [Why should you remove unnecessary C# using directives?](http://stackoverflow.com/questions/136278/why-should-you-remove-unnecessary-c-using-directives) – ChrisF Jul 27 '10 at 20:28

4 Answers4

100

From The C# Team's answers to frequently asked questions:

When you add assembly references or make use of the 'using' keyword, csc.exe will ignore any assembly which you have not actually made use of in your code ... Don't [waste] your time stripping out unused 'using' statements or assembly references from your application. The C# compiler will do so for you automatically.

You can verify that this is actually the case by calling Assembly.GetReferencedAssemblies(); you'll see that anything that isn't used won't actually be included in the list.

The main utility in stripping out unused ones is

  • It is easier to see what your code is actually using
  • It will keep your Intellisense from being polluted with things that you aren't actually going to use.
Daniel LeCheminant
  • 50,583
  • 16
  • 120
  • 115
41

There are some performance reasons to strip down using statements:

  • VS's IntelliSense runs faster when there is less data to look through

However, note all these reasons are compile-time specific. The compiler strips unused usings automatically.

strager
  • 88,763
  • 26
  • 134
  • 176
11

The C# code editor in Visual Studio 2008 has a feature to remove unused using statements.

Right-click and select Organize Usings | Remove Unused Usings.

Rob Windsor
  • 6,744
  • 1
  • 21
  • 26
10

The biggest "overhead" in unusesd using statements is understanding your code :)

Removing unused ones makes your code tidier and easier for somebody else to read and maintain.

Craig Shearer
  • 14,222
  • 19
  • 64
  • 95