Be Mindful of the References / 'using' / Manifest Relationship

Given that the .NET platform encourages binary reuse of types, it is commonplace to set references to external assemblies using the Visual Studio .NET Add Reference dialog box. Many programmers (especially those of the C(++) ilk) fear that adding unnecessary external references can result in a bit of 'code bloat'. Nothing could be further from the truth. When you add assembly references or make use of the 'using' keyword, csc.exe will ignore any assembly which you have not actually made use of in your code. Thus, if you were to set a reference to System.Data.dll and System.Windows.Forms.dll but only authored the following code:

 using System;
using System.Data;  // Ignored. 
using System.Windows.Forms; // Ignored. 

public class MyClass
{
  public static void Main()
  {
    Console.WriteLine("Hi there.");
  }
}

the compiler would only reference the mandatory mscorlib.dll.

As you may be aware, when you open up a .NET assembly using ildasm.exe, the MANIFEST icon may be double clicked to open a window describing the binary under investigation. At the very top, you will see a list of each external assembly the current assembly was compiled against (provided that it was actually used):

 .assembly extern mscorlib
{ … }

Bottom line? Don't waist your time stripping out unused 'using' statements or assembly references from your application. The C# compiler will do so for you automatically.


Tip from Andrew Troelsen

Posted by: Duncan Mackenzie, MSDN

This post applies to Visual C# .NET 2002/2003/2005