Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance impact of unused "using" directives in C#

Tags:

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.

like image 997
yogi Avatar asked Jan 29 '13 10:01

yogi


2 Answers

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.

like image 168
VinayC Avatar answered Oct 14 '22 04:10

VinayC


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
like image 28
Androiderson Avatar answered Oct 14 '22 03:10

Androiderson