Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 207
Cerebrus Avatar asked Mar 07 '09 10:03

Cerebrus


People also ask

How do I delete unused packages in Visual Studio?

Clean up project references and NuGet packages in Visual Studio. Firstly, Right Click on the Project, and select “Remove Unused References”. This will bring up the “Remove Unused References” Dialog that shows the projects and all unused packages. Here, you can again choose if you want to remove them or keep it as it is ...

How do I know if my NuGet package is unused?

Use ReSharper or similar code analysis tool to identify any unused references in your projects and uninstall the nuget in the corresponding projects. Sometimes uninstalled nugets still linger in the Installed packages and Updates lists in the Manage NuGet Packages dialog.

How do I delete all unused imports in Visual Studio?

Shift + Alt + O will take care of you. If you're a heavy visual studio user, you can simply open your preference settings and add the following to your settings.

Where is the unused code in Visual Studio?

To find unused members with a Code Analysis Ruleset, from the Visual Studio menu select File -> New -> File… -> General -> Code Analysis Rule Set. Uncheck all the rules. There are many rules we don't care about right now – and some we probably won't ever care about.


1 Answers

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...

like image 123
Shea Avatar answered Oct 13 '22 21:10

Shea