为什么没有警告这个未使用的变量提供? [英] Why is no warning given for this unused variable?

查看:132
本文介绍了为什么没有警告这个未使用的变量提供?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS2010中,在Windows VS2008或MonoDevelop的编译下面的程序,我得到警告的 CS0219 变量Y被分配但其值从未使用过。

When compiling the following program in VS2010, VS2008 or MonoDevelop on Windows, I get warning CS0219, "The variable 'y' is assigned but its value is never used".

namespace Problem
{
    public class Program
    {        
        private static void Main(string[] args)
        {
            object x = new object();
            int y = 0;
        }
    }
}



为什么没有预警 X在Visual Studio编译时?

有趣的是,我得到CS0219警告 X 的在MonoDevelop的Mac OS X上编译时。

Interestingly, I do get CS0219 warnings for x and y when compiling in MonoDevelop on Mac OS X.

推荐答案

原来,当赋值操作的右手边是不是编译时间常数此警告被抑制。

It turns out that this warning is suppressed when the right-hand-side of the assignment operation is not a compile-time constant.

微软解释说那是因为他们有很多的投诉,谁被分配变量纯属这样他们就可以看到调试过程中有什么返回的方法调用的人,找到了报警刺激性:

Microsoft explain that it's because they had lots of complaints from people who were assigning variables purely so they could see what a method call returned during debugging, and found the warning irritating:

已分配但从未使用,在这种情况下,
警告被反馈谁做到这一点的用户激励的抑制:

The suppression of the "assigned but never used" warning in this case was motivated by feedback from users who do this:

int Blah(){
    // blah
    BlahBlah(x, y, z)
    // blah
    // blah
}

嘿,用户说我在调试,想知道BlahBlah是
回来?但有研究在
调试器的返回值没有简单的方法,让用户非常频繁做到这一点:

"Hey," says the user while debugging, "I wonder what BlahBlah is returning?" But there is no easy way to examine the return value in the debugger, so users very frequently do this:

int Blah()
{
    // blah
    int temp = BlahBlah(x, y, z)
    // blah
    // blah
}

然后用当地人或观看窗口来检查温度。临时为
从未使用过其他地方的功能,所以它产生的刺激性
分配,但不读的警告。

and then use the locals or watch window to examine temp. The temp is never used anywhere else in the function, so it produced an irritating "assigned but not read" warning.

我觉得这是因为有点羞愧:

I think this is a bit of a shame since:


  1. 其实我觉得这些警告有帮助,当他们在MonoDevelop中给出

  2. 任何人都可以抑制警告自己(当然他们会还可以抑制未使用的编译时间常数分配的那些 - 也许应该有一个单独的警告?)

无论如何,我明白,你不能取悦所有人。

Anyway, I understand that you can't please everyone.

这篇关于为什么没有警告这个未使用的变量提供?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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