为什么在 VB.NET 和 C# 中根据值检查 null 会有所不同? [英] Why is there a difference in checking null against a value in VB.NET and C#?

查看:26
本文介绍了为什么在 VB.NET 和 C# 中根据值检查 null 会有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VB.NET 中会发生这种情况:

In VB.NET this happens:

Dim x As System.Nullable(Of Decimal) = Nothing
Dim y As System.Nullable(Of Decimal) = Nothing

y = 5
If x <> y Then
    Console.WriteLine("true")
Else
    Console.WriteLine("false") '' <-- I got this. Why?
End If

但在 C# 中会发生这种情况:

But in C# this happens:

decimal? x = default(decimal?);
decimal? y = default(decimal?);

y = 5;
if (x != y)
{
    Debug.WriteLine("true"); // <-- I got this -- I'm with you, C# :)
}
else
{
    Debug.WriteLine("false");
}

为什么会有不同?

推荐答案

VB.NET 和 C#.NET 是不同的语言,由不同的团队构建,他们对用法做了不同的假设;在这种情况下,NULL 比较的语义.

VB.NET and C#.NET are different languages, built by different teams who have made different assumptions about usage; in this case the semantics of a NULL comparison.

我个人更喜欢 VB.NET 语义,它本质上给了 NULL 语义我还不知道".然后将 5 与我还不知道"进行比较.自然是我还不知道";即 NULL.这具有在(大多数,如果不是全部)SQL 数据库中镜像 NULL 行为的额外优势.这也是对三值逻辑的更标准的(比 C# 的)解释,如here.

My personal preference is for the VB.NET semantics, which in essence gives NULL the semantics "I don't know yet". Then the comparison of 5 to "I don't know yet". is naturally "I don't know yet"; ie NULL. This has the additional advantage of mirroring the behaviour of NULL in (most if not all) SQL databases. This is also a more standard (than C#'s) interpretation of three-valued logic, as explained here.

C# 团队对 NULL 的含义做出了不同的假设,从而导致您表现出的行为差异.Eric Lippert 写了一篇关于C#中NULL的含义.Per Eric Lippert:我还写了关于 VB/VBScript 和 JScript 中空值的语义的文章 此处在这里".

The C# team made different assumptions about what NULL means, resulting in the behaviour difference you show. Eric Lippert wrote a blog about the meaning of NULL in C#. Per Eric Lippert: "I also wrote about the semantics of nulls in VB / VBScript and JScript here and here".

在任何可能出现 NULL 值的环境中,重要的是要认识到排中律(即 A 或 ~A 重言式为真)不再可靠.

In any environment in which NULL values are possible, it is imprtant to recognize that the Law of the Excluded Middle (ie that A or ~A is tautologically true) no longer can be relied on.

更新:

bool(相对于 bool?)只能取值 TRUE 和 FALSE.然而,NULL 的语言实现必须决定 NULL 如何通过表达式传播.在 VB 中,表达式 5=null5<>null 都返回 false.在 C# 中,在可比较的表达式 5==null5!=null 中,只有 second 第一个 [更新 2014-03-02 - PG] 返回假.但是,在任何支持 null 的环境中,程序员都有责任了解该语言使用的真值表和 null 传播.

A bool (as opposed to a bool?) can only take the values TRUE and FALSE. However a language implementation of NULL must decide on how NULL propagates through expressions. In VB the expressions 5=null and 5<>null BOTH return false. In C#, of the comparable expressions 5==null and 5!=null only the second first [updated 2014-03-02 - PG] returns false. However, in ANY environment that supports null, it is incumbent on the programmer to know the truth tables and null-propagation used by that language.

更新

Eric Lippert 关于语义的博客文章(在下面的评论中提到)现在位于:

Eric Lippert's blog articles (mentioned in his comments below) on semantics are now at:

十月.2003 年 1 月 - 无所事事

这篇关于为什么在 VB.NET 和 C# 中根据值检查 null 会有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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