-1

I have a C# code file which has few using directives. Out of these, the classes from some of these are never used. Would it affect performance if those using directives are left in the code?

In the example shown below, the classes from System.Linq are never used, would it hurt performance if I leave it there in the code, especially in the case where there are many such files?

enter image description here

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Sharath
  • 516
  • 1
  • 8
  • 19

2 Answers2

3

There aren't any performance benefits, if that's what you mean.

All references in an assembly are fully qualified; the compiler merely uses the references you provide in your code to fully qualify identifiers, so the only impact of unused references in your source code is a slight decrease in readability (why is this reference here?), and a trivial increase in compile time.

1

The answer is no.

At worst, referenced assemblies are not immediately loaded - they are loaded on the fly as needed. In the case they are not needed at all, then they are never loaded and omitted completely. Having an unused using directive laying around, is neither using a referenced assembly, or loading them, and optimised out when compiled (for lack of better words), in reality the using never existed... insert eerie music.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • _"and optimised out when compiled"_ -- this answer implies that a `using` directive has some material effect after compilation when they _do_ have an effect. That's simply false. They aren't "optimized out". They simply don't even exist after compilation, whether or not they were needed for the code. – Peter Duniho Feb 28 '20 at 06:20
  • @PeterDuniho i can see why you would say that, probably a bad choice of words – TheGeneral Feb 28 '20 at 06:21