Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
Comments
- Anonymous
October 20, 2004
The comment has been removed - Anonymous
October 21, 2004
The comment has been removed - Anonymous
October 22, 2004
ReSharper can remove unused "using" references. - Anonymous
October 23, 2004
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. - Anonymous
October 23, 2004
There are some tips about C# Written By Andrew Troelsen - Anonymous
December 27, 2004
[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/] - Anonymous
May 11, 2008
PingBack from http://waxtadpole.wordpress.com/2008/05/12/unused-usings/