22

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.

Jacob
  • 2,212
  • 1
  • 12
  • 18
yogi
  • 19,175
  • 13
  • 62
  • 92
  • Why would you do that anyway? – DGibbs Jan 29 '13 at 10:23
  • 4
    No it is just impact your compilation time. – Hamlet Hakobyan Jan 29 '13 at 10:23
  • 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). – Matthew Watson Jan 29 '13 at 10:24
  • @DGibbs , 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 ?? – yogi Jan 29 '13 at 10:26
  • 2
    If you use resharper you can use it to find and remove unreferenced `using` statements. http://www.jetbrains.com/resharper/webhelp/Code_Cleanup__Usage_Scenarios__Optimizing_Using_Directives.html – mortb Jan 29 '13 at 10:26
  • 8
    Terminology nitpick - these are `using` **directives**. `using` **statements** are the ones which appear in methods etc, to automatically call `Dispose`. – Jon Skeet Jan 29 '13 at 10:29
  • 3
    [CodeMaid](http://visualstudiogallery.msdn.microsoft.com/76293c4d-8c16-4f4a-aee6-21f83a571496), which is *free* unlike resharper, can "clean up" a whole solution. You can easily remove all unused `using`. That said, as [VinayC said](http://stackoverflow.com/a/14580666/588868), `using` is only a compiler helper. – Steve B Jan 29 '13 at 10:29
  • @JonSkeet: yes that should be *directives* of course :) – mortb Jan 29 '13 at 10:49
  • 1
    Possible duplicate of [Why should you remove unnecessary C# using directives?](http://stackoverflow.com/questions/136278/why-should-you-remove-unnecessary-c-sharp-using-directives) – azam Mar 07 '17 at 02:10

4 Answers4

19

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.

VinayC
  • 47,395
  • 5
  • 59
  • 72
  • 8
    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. – TomTom Jan 29 '13 at 10:26
14

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
Androiderson
  • 16,865
  • 6
  • 62
  • 72
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.

Captain Kenpachi
  • 6,960
  • 7
  • 47
  • 68
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).

Botz3000
  • 39,020
  • 8
  • 103
  • 127