.netvisual-studioreference

What is the purpose of "remove unused references"


I have read that removing unused references makes no difference to the compiler as it ignores assemblies that are not being referenced in the code itself.

But I find it hard to believe because then, what is the real purpose of Removing unused references? It doesn't have any noticeable effect on the size of the generated assembly or otherwise. Or is this smart behaviour limited to the C# compiler (csc.exe) and not inherent to vbc.exe?

If this functionality is so useless, why does ReSharper offer it as a feature? Why is it provided within the Visual Studio Project Configuration dialog?

The only activity I can think of where this would be useful is during Deployment. References (used or unused) would still be copied by the installer. But for assemblies that reside in the GAC (for instance, BCL assemblies), this would not be a problem either.


Solution

  • It prevents the CLR from loading the referenced module at runtime. This will reduce startup time (since it takes time to load each module). Depending on the size of the module it might noticeably reduce startup time.

    One way to test this is to create a test WinForms project, add a reference to an assembly that isn't used (e.g., System.Web) then run and attach to the executable (e.g., F5). View the loaded modules (Debug -> Windows -> Modules) and you'll see the referenced assembly was loaded.

    If you think about it, it would be pretty hard for the CLR to determine whether or not a dependency (it's in the manifest as a dependency once you add a reference to it) is really used... Especially since the execution of some code paths can't be known in advance...