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

Published 20 October 04 04:58 PM

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

Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Ruben Bartelink said on October 21, 2004 3:06 AM:
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.
# Anthony Ashe said on October 21, 2004 5:45 PM:
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?
# Chinh Do said on October 22, 2004 8:17 AM:
ReSharper can remove unused "using" references.
# Wayfarer's Prattle said on October 23, 2004 11:28 AM:
There are some tips about C# Written By Andrew Troelsen
# Bruce Zhang said on October 23, 2004 8:57 AM:
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.
# RebelGeekz said on December 28, 2004 4:54 AM:
[http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/]
# FAQ C# said on April 19, 2005 5:58 AM:
# FAQ C# (par Yannick Lejeune) said on August 23, 2005 5:30 AM:
# Unused usings « Bite The Wax Tadpole said on May 12, 2008 1:17 AM:

PingBack from http://waxtadpole.wordpress.com/2008/05/12/unused-usings/

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker