0

What is the difference between :

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

and

using System;
using System.Collections.Generic;

Does it really affect performance when adding "too much" references ? For big applications, does is there a difference by removing useless references ?

I mean, if I use "using System" it seems to be enough to cover most of my requirements, so why should I be specific by adding specific references under System reference?

Thanks

expirat001
  • 2,125
  • 4
  • 30
  • 40
  • 1
    Visual Studio (or is it the ReSharper extension?) shows less grayed out lines of "unused references" :} There is *no difference* in the compiled code - the types are fully resolved; the 'usings' just tell the compiler where they should resolve from. As with all code, I recommend good house-keeping. – user2864740 May 28 '16 at 20:56
  • @user2864740 No extension is required for this functionality. – user1751825 May 28 '16 at 21:08
  • @user1751825 Good to know - it's been a very long time since I've used a Vanilla VS install :D – user2864740 May 28 '16 at 21:25
  • I use Visual Studio Community 2015 and I confirm that no extension is required to have the "Show potential fixes" which allows me to remove unused references. – expirat001 May 28 '16 at 21:33

5 Answers5

2

By using using you are including namespaces, not referencing the assemblies.

Also it is important to be as specific as possible to avoid naming conflicts. Please take a look at this basic namespace covering MSDN article

And regarding performance

Community
  • 1
  • 1
Peuczynski
  • 4,591
  • 1
  • 19
  • 33
  • OK, now I understand. So the goal is to prevent possible conflicts, if for example two namespaces have the same class "File", so I reduce the conflict. Thanks, I am going to read the article you posted here. – expirat001 May 28 '16 at 21:05
  • If you use a namespace in using directive you can use the namespace classes directly so if you have using System.IO at the beginning you can just use File class. If you woulnd't have it decrared in using directive you could just use is with prefix like System.IO.File but you have to have the assembly referenced – Peuczynski May 28 '16 at 21:09
2

It's a matter of code readability. Yes, maybe right now while you're writing the code, you know that you aren't using System.Linq and so its presence doesn't bother you. After all, one day you might use it, right?

But some day somebody else is going to be reading and maintaining your code, or you will, long enough afterwards that you've forgotten what it contains, and they'll (or you will) be hunting down a bug in your Linq calls, and your reference to System.Linq will imply that file was built to use Linq, which will waste their time. Yes, probably only a little time, but a little time adds up fast. What do you do when you do a file search for "Linq" and you get 1,000 results when you only actually used it 5 times?

Never reference code you aren't using. When you decide to use it, then reference it. For every time you reference unnecessary code "because maybe I might use it someday," there will be fifty times other developers see it and think maybe you used it.

  • Regarding the `System.Linq` remember that you can always find the missing namespaces via Ctrl+. but Linq is extensions method library so I can be confusing for a beginner programmer why there are less methods in IEnumerable objects – Peuczynski May 28 '16 at 21:11
2

This question has been answered here and here, and probably a few other places.

The main takeaway is that they don't have a performance impact on the running application, but they do affect compile time. That's probably the biggest one, where "performance" may be concerned.

That said, it keeps your code clean. It may matter little for your personal projects, where only you ever see your code. However, if you're in a professional environment where another person will inevitably have to work with your code, unnecessary usings may cause confusion, especially with junior developers.

There are other issues, which I encourage you to read those questions to learn about.

Community
  • 1
  • 1
Travis
  • 1,044
  • 1
  • 17
  • 36
1

One reason to include more specific "using" references would be to make the rest of the code less verbose. It makes no difference though to the compilation or performance of the application.

Including unused references though is pointless, and should be avoided.

user1751825
  • 4,029
  • 1
  • 28
  • 58
0

Of course the referenced assembles are omitted if are not used anywhere in code. In addition, parts of the code that are not in use are omitted from the compilation as well. For example if you define an Int, set it, but don't use anywhere, it will be omitted from the compilation.

papapa
  • 3
  • 1