价值分配的,从未使用过,但没有编译器消息 [英] value assigned, never used, but no compiler message

查看:144
本文介绍了价值分配的,从未使用过,但没有编译器消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET Web的应用程序的Web表单的整个程序:

The whole program in a web form of a .net web-application:

namespace WebApplication1
{
   public partial class WebForm1 : System.Web.UI.Page
   {
     protected void Page_Load(object sender, EventArgs e)
     {
        string x = "";
        string y = String.Empty;
     }
   }
}

如果我构建应用程序,编译强调的x,

If I build the application, the compiler underlines x,

变量x分配给但汇入作业值从未使用

The variable x is assigned to but it´s value is never used

有关Y,我没有得到任何下划线。为什么不? (VS 2008,NET 3.5的)

For y, I get no underlining. Why not? (VS 2008, .Net 3.5)

推荐答案

我详细解答您的问题在这里:

I answer your question in detail here:

http://stackoverflow.com/a/2741281/88656

简而言之:编译器检测到本地写入,但流分析传递期间不读。它刻意抑制了警告,如果写入到本地的值是一个非恒定。 的String.Empty 不是恒定的,它是一个只读字段,奇怪。但空字符串的的常数。这就是为什么你看到一个用文字而不是字段的警告。

Briefly: the compiler detects that the local is written to but not read during the flow analysis pass. It deliberately suppresses the warning if the value written to the local is a non-constant. String.Empty is not a constant, it is a read-only field, oddly enough. But the empty string literal is a constant. That is why you see the warning for the one with the literal but not for the field.

编译器是推理,你可能表达式的值赋给一个未读 - 从本地的为了方便调试程序的。我们不希望你有把警告是错误的关,每次你介绍一个解释变量,以协助调试。在这种情况下,显然你不使用变量来考察的String.Empty 是建立在编译器失去了输出的事实;它不知道什么字段引用的语义。

The compiler is reasoning that you might be assigning the value of the expression to an unread-from local in order to facilitate debugging the program. We do not want you to have to turn "warnings are errors" off every time you introduce an explanatory variable to assist in debugging. The fact that in this case, obviously you are not using the variable to examine the output of String.Empty is lost upon the compiler; it does not know what the semantics of the field reference are.

这篇关于价值分配的,从未使用过,但没有编译器消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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