6

I have been using ReSharper for quite a long time and get used to solve a lot of problems by typing Alt+Enter. One of my colleague asked me about the real benefits and I couldn't say a word apart from that if you are not using it why import it.

Can someone explain what are the real benefits of removing redundant imports if there are eany?

Andreas
  • 5,393
  • 9
  • 44
  • 53
Ybbest
  • 1,520
  • 3
  • 29
  • 43

6 Answers6

6

Beside the fact that it cleans up your code, you can also minimize 'name clashes'.
For instance, when you have 2 types with the same name in different namespaces, and you do not use any type from one namespace ... You then do not have to specify the namespace of the type when using it, it keeps your code cleaner.

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
4

It removes redundancy, which is a good thing. And its easy to get most of the needed using directives back, by pressing ALT+ENTER (with R#) or invoking the smart-tag by pressing CTRL+. (without R#) on the class that needs it.

On the negative side, you might lose sight of available extension methods, especially when the System.Linq namespace import is removed.

Arjan Einbu
  • 13,543
  • 2
  • 56
  • 59
2

It'll reduce the size of your source code (very slightly), but also improves code quality in my opinion. If you only include what you need, it makes the intent of the file that much clearer.

If you're including a library you don't need, and I'm reading your code, I might end up wasting time trying to figure out where you reference it or why it's used.

Sapph
  • 6,118
  • 1
  • 29
  • 32
1

If you import more than you need you run into a few issues:

  • When you try to use a class, it is ambiguous which class you mean and you have to specify further to disambiguate
  • Autocomplete suggests many things that you're unlikely to use (it typically does this anyway, but having extra usings just makes the problem worse).
  • It's hard to tell which dependencies your class has just from looking at the using list.
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
0

It is faster -- if you have a reference the compiler has to load the symbol tables.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • 1
    The topicstarter is not talking about references to assemblies, he talks about importing/using namespaces that are not used. – Frederik Gheysels Jan 05 '10 at 22:11
  • Yes I know that. How do you think compilers are able to resolve the names when you use the import statement. The compiler goes and reads a symbol table for that namespace and places the names in the symbol table so it can resolve them. I don't mind getting a -1 when wrong, but this is compiler writing 101. – Hogan Jan 05 '10 at 22:16
  • Here you go @Frederik someone else who said the same thing http://stackoverflow.com/questions/136278/why-should-you-remove-unnecessary-c-using-directives/136320#136320 – Hogan Jan 05 '10 at 22:20
0

There are no runtime or compilation performance benefits, at all. It just makes for prettier code.

Randolpho
  • 55,384
  • 17
  • 145
  • 179
  • Wouldn't the compilation be slower if more symbols are loaded? – Hogan Jan 05 '10 at 22:12
  • Perhaps, but only marginally so; the human eye would rarely be able to tell the difference. You'll have a lot more performance problems from loading referenced projects during compilation than from unnecessary using statements. – Randolpho Jan 05 '10 at 22:44