Performance impact of unused "using" directives in C#
Asked Answered
S

4

24

Just curious about it.

Does it matter if I add multiple using directives at the starting of my code file which I don't use in my code. Like this.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
//using blah.. blah.. blah..;

public class myClass
{
    // Class members
}
  • Does it have a bad impact on my application's memory usage?

  • Does it have a bad impact on my application's performance?

I know it is a good practice to remove them and we have short full support of .Net IDE to do so, but I am just curious to know about it.

Spirograph answered 29/1, 2013 at 10:22 Comment(9)
Why would you do that anyway?Avlona
No it is just impact your compilation time.Swallowtail
It has no effect. What does have an effect is adding a reference to a DLL that you don't use - because you'll get a DLL in your output folder which you don't need and which takes up disk space. If you have a using for a type in a referenced DLL that is not otherwise used, you may think it is used because if you remove the reference you'd get a compile error (but it would just be the unneeded using causing it).Conservation
@Avlona , suppose I had some requirements from a client according to which I code and added some using but now requirement changes for the application and code changes too, So should I invest time on finding and removing those unwanted using ??Spirograph
If you use resharper you can use it to find and remove unreferenced using statements. jetbrains.com/resharper/webhelp/…Tomika
Terminology nitpick - these are using directives. using statements are the ones which appear in methods etc, to automatically call Dispose.Muriah
CodeMaid, which is free unlike resharper, can "clean up" a whole solution. You can easily remove all unused using. That said, as VinayC said, using is only a compiler helper.Storer
@JonSkeet: yes that should be directives of course :)Tomika
Possible duplicate of Why should you remove unnecessary C# using directives?Majorette
E
20

Extra Using directives will not have any memory/performance impact on final application - they are nothing but compiler provided shortcuts to deal with long type names. Compiler uses these namespaces to resolve unqualified (or partly qualified) type names to correct types.

Emendate answered 29/1, 2013 at 10:24 Comment(1)
More exact: There is no concept of "using" in .NET CLR. This is purely a compiler help so you dont have to type full namespace. Post compiler, every class you reference (and only those) is linked with full name, no trace of using is left in the generated bytecode.Nomenclator
B
15

Just for the sake of completeness, the IL generated for this:

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Hello World!");
        }
    }
}

and this:

class Program
{
    static void Main(string[] args)
    {
        System.Console.Write("Hello World!");
    }
}

are exactly the same:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       13 (0xd)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldstr      "Hello World!"
  IL_0006:  call       void [mscorlib]System.Console::Write(string)
  IL_000b:  nop
  IL_000c:  ret
} // end of method Program::Main
Broadleaved answered 29/1, 2013 at 11:38 Comment(0)
P
8

There are no performance hits on your application. It's just a shortcut you use to avoid typing the entire qualification. e.g.

var f = new File()

instead of

var f= new System.IO.File();

HOWEVER. it DOES impact the performance of your development environment (IDE) somewhat because the more using statements you use, the larger the auto-complete cache grows. This makes lookup times slightly slower. But this is usually hardly noticeable.

This advice doesn't apply to adding assembly references to your project though. If you add a reference to MyGloriousLibrary.DLL and never use it, you're gonna have a bad time.

Prevention answered 29/1, 2013 at 10:30 Comment(0)
G
4

It doesn't affect the overall performance or memory usage of your application at all. The using directives are just there at compile time, so that you don't have to write out the full class name every time. There is nothing left of those directives once your code is compiled (the compiled code always uses full type names).

Gillie answered 29/1, 2013 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.