visual-studio 从 .NET 代码中删除未使用的局部变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2492398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing unused local variables from .NET code
提问by Jesse MacVicar
Is there a tool available that can scan a C# or VB.NET project and automatically remove all unused local variables?
是否有可用的工具可以扫描 C# 或 VB.NET 项目并自动删除所有未使用的局部变量?
采纳答案by Randolpho
Ahem: FxCop and ReSharper are nice and all, but the compiler will happily ignore unused variables and never even declare them in compiled code if you enable the optimizeoption.
咳咳:FxCop 和 ReSharper 都不错,但编译器会很乐意忽略未使用的变量,如果启用该optimize选项,甚至不会在编译代码中声明它们。
回答by Anton
You can use ReSharper. It will mark all unused variables and allow you to remove them.
您可以使用 ReSharper。它将标记所有未使用的变量并允许您删除它们。
回答by Bryan Denny
It looks like ReSharper should help you clean up your code.
看起来 ReSharper 应该可以帮助您清理代码。
回答by Ivo
回答by Grant Palin
FxCop is a useful tool in a number of ways, one of which is that it will point out unused variables and parameters. This is helpful if you have your code going through a continuous integration process.
FxCop 在许多方面都是一个有用的工具,其中之一是它会指出未使用的变量和参数。如果您的代码经历了持续集成过程,这将很有帮助。
ReSharper will highlight unused variables and parameters when you are in Visual Studio.
当您在 Visual Studio 中时,ReSharper 将突出显示未使用的变量和参数。
Neither tool will automatically do the removal for you, although it's usually a simple thing to do...R# even provides a shortcut command to remove a single selected variable for you.
这两个工具都不会自动为您删除,尽管这通常是一件简单的事情……R# 甚至提供了一个快捷命令来为您删除单个选定的变量。

![visual-studio :错误 C2664:“MessageBoxW”:无法从“const char [40]”转换参数 2](/res/img/loading.gif)