C# FAQ: Do unused references to external assemblies lead to code bloat

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

Do unused references to external assemblies lead to code bloat?

The .NET platform promotes the binary reuse of types. It is common practice to set references to external assemblies using Visual Studio .NET's Add Reference menu item. Many developers believe that adding unnecessary and, therefore, unused external references will result in code bloat—binary code loaded into the program even though unused. But, in fact, when assembly references are added or the using keyword is used, csc.exe—the C# compiler—ignores assemblies which are not actually used by the application.

For example, if references to System.Net.dll and System.Reflection.dll were added, but were not otherwise referenced in the application, the compiler would only reference the mandatory mscorlib.dll. Inspect the following sample code:

using System;
using System.Net;          // Ignored by C# compiler. 
using System.Reflection;   // Ignored by C# compiler. 
 
public class TestClass
{
  public static void Main()
  {
    Console.WriteLine ("No code bloat here!");
  }
}

When a .NET assembly is opened using ildasm.exe, the MANIFEST icon can be double clicked, opening a window describing the binary being examined. A list of external assemblies compiled with the current assembly appears near the top:

.assembly extern mscorlib
{}

So, it is a waste of time to strip out unused assembly references and using statements from an application; because, the C# compiler does this automatically. However, many excellent programmers consider it poor form to include unused references in source code.


Personal tools