C# Frequently Asked Questions

The C# team posts answers to common questions

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

Published Wednesday, October 20, 2004 4:58 PM by CSharpFAQ
Filed under:

Comments

 

Ruben Bartelink said:

s/waist/waste/
I would qualify the statement "Don't waste your time stripping" by saying "It doesnt provide a performance or code size benefit to strip" as there are [non-performance] benefits to having a tidily organised using section.
October 21, 2004 3:06 AM
 

Anthony Ashe said:

While it might not impact performance, Visual Studio still does a "copy local" on these unused assemblies, which moves them to your bin folder. So if you have automated builds and installer creation, it is hard to avoid including this unused crap in your installer. I haven't managed to find a tool that automates the process of getting rid of unused references though. Any ideas?
October 21, 2004 5:45 PM
 

Chinh Do said:

ReSharper can remove unused "using" references.
October 22, 2004 8:17 AM
 

Wayfarer's Prattle said:

There are some tips about C# Written By Andrew Troelsen
October 23, 2004 11:28 AM
 

Bruce Zhang said:

Absolutely right. In fact, we can verify it simply, that is, using Assembly.GetReferencedAssemblies() method.

As you say, when we set a reference to System.Data.dll and System.Windows.Forms.dll, but don't use them. if we compile your example code to Assembly file, then we get the information of its referenced assemblies through loading it by Assemlby.LoadFrom(). Now only mscorlib.dll will be listed without System.Data.dll and System.Windows.Forms.dll.
October 23, 2004 8:57 AM
 

RebelGeekz said:

December 28, 2004 4:54 AM
 

FAQ C# said:

April 19, 2005 5:58 AM
 

FAQ C# (par Yannick Lejeune) said:

August 23, 2005 5:30 AM
 

Unused usings « Bite The Wax Tadpole said:

May 12, 2008 1:17 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker