有没有办法让VS2008停止警告我有关无法访问的代码? [英] Is there a way to get VS2008 to stop warning me about unreachable code?

查看:48
本文介绍了有没有办法让VS2008停止警告我有关无法访问的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一些配置选项,类似于

I have a few config options in my application along the lines of

const bool ExecuteThis=true;
const bool ExecuteThat=false;

,然后像这样使用它的代码

and then code that uses it like

if(ExecuteThis){ DoThis(); }
if(ExecuteThat){ DoThat(); } //unreachable code warning here

问题是,我们可能会发布稍有不同的版本,而不是ExecuteThis或ExecuteThat,我们希望能够使用const,这样我们就不会在运行时受到此类速度的任何不利影响。但是我厌倦了看到有关无法访问代码的警告。我是一个喜欢消除所有警告的人,但我对此无能为力。我可以使用一些选项来关闭这些警告吗?

The thing is, we may make slightly different releases and not ExecuteThis or ExecuteThat and we want to be able to use consts so that we don't have any speed penalties from such things at run time. But I am tired of seeing warnings about unreachable code. I'm a person that likes to eliminate all of the warnings, but I can't do anything about these. Is there some option I can use to turn just these warnings off?

推荐答案

要禁用:

#pragma warning disable 0162

要还原:

#pragma warning restore 0162

有关 #pragma警告的更多信息,请参见 MSDN

For more on #pragma warning, see MSDN.

请注意,对C#编译器的优化足以避免发出无法访问的代码。这称为死代码消除,它是 C#编译器执行的一些优化

Please note that the C# compiler is optimized enough to not emit unreachable code. This is called dead code elimination and it is one of the few optimizations that the C# compiler performs.

而且您不应该随意禁用警告。警告是问题的征兆。请参阅此 answer

And you shouldn't willy-nilly disable the warnings. The warnings are a symptom of a problem. Please see this answer.

这篇关于有没有办法让VS2008停止警告我有关无法访问的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆