结构比较时,空错了编译器警告 [英] Wrong compiler warning when comparing struct to null

查看:153
本文介绍了结构比较时,空错了编译器警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的代码:

DateTime t = DateTime.Today;

bool isGreater = t > null;



使用Visual Studio 2010(C#4,.NET 4.0),我得到以下警告:

With Visual Studio 2010 (C# 4, .NET 4.0), I get the following warning:

警告CS0458:表达式的结果总是类型的'空'布尔?

warning CS0458: The result of the expression is always 'null' of type 'bool?'

这是不正确;结果总是 (类型为布尔):

This is incorrect; the result is always false (of type bool):

现在,该结构的DateTime重载> (大于)运算符。任何非空的结构(如DATETIME)隐式转换为相应的可空<> 键入。上述表达式是完全等同于

Now, the struct DateTime overloads the > (greater than) operator. Any non-nullable struct (like DateTime) is implicitly convertible to the corresponding Nullable<> type. The above expression is exactly equivalent to

bool isGreater = (DateTime?)t > (DateTime?)null;



这也产生同样的错误的警告。在这里,> 运营商是在取消运营商。该作品以返回false的任何两个操作数,如果的HasValue 。否则,提升操作员将进行两个操作数解开到底层结构,然后调用的过载> 通过该结构定义(但这不是必要的,这其中的一个操作数不的HasValue )。

which also generates the same wrong warning. Here the > operator is the lifted operator. This works by returning false if HasValue of any of its two operands is false. Otherwise, the lifted operator would proceed to unwrap the two operands to the underlying struct, and then call the overload of > defined by that struct (but this is not necessary in this case where one operand does not HasValue).

你可以重现这个bug,并且是这个bug知名?我误解的东西吗?

这是所有结构类型(而不是简单的类型,如相同的 INT 而不是枚举类型)的过载问题的运营商。

This is the same for all struct types (not simple types like int, and not enum types) which overload the operator in question.

(现在如果我们使用 == 代替的> ,一切都应该是完全相似(因为日期时间也重载了 == 运营商),但事实并非如此。 。类似的如果我说

(Now if we use == instead of >, everything ought to be entirely similar (because DateTime also overloads the == operator). But it's not similar. If I say

DateTime t = DateTime.Today;

bool isEqual = t == null;

我得到的没有警告☹有时候你看到人们意外地检查变量或为空参数,并没有意识到,他们的变量的类型是一个结构(该重载 == 键,这不是一个简单的类型,比如 INT ),这将是更好,如果他们得到了一个警告。)

I get no warning ☹ Sometimes you see people accidentally check a variable or parameter for null, not realizing that the type of their variable is a struct (which overloads == and which is not a simple type like int). It would be better if they got a warning.)

更新:用C#编译器6.0(基于罗斯林)的Visual Studio 2015,与 isGreater 不正确的消息,上述变更为CS0464具有正确的和有益的警告消息。此外,缺乏与的isEqual 上面的警告被固定在VS2015的编译器,但只有当你用 /功能编译:严格

Update: With the C# 6.0 compiler (based on Roslyn) of Visual Studio 2015, the incorrect message with isGreater above is changed into a CS0464 with a correct and helpful warning message. Also, the lack of warning with isEqual above is fixed in VS2015's compiler, but only if you compile with /features:strict.

推荐答案

您是正确的:这是在Visual Studio 。在C#4.0标准(第7.3.7抬升经营者)有这样一段话:

You are correct: this is a bug in Visual Studio. The C# 4.0 standard (§ 7.3.7 Lifted operators) has this to say:

有关关系运算符

<  >  <=  >=

[...] 提升运算的结果为如果一个或两个操作数为空。 ...

[…] The lifted operator produces the value false if one or both operands are null.

和事实上在MonoDevelop中,你会得到以下警告,而不是:

And in fact, in MonoDevelop, you get the following warning instead:

比较型的结果的System.DateTime 总是

这篇关于结构比较时,空错了编译器警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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