首页 > 解决方案 > = 运算符应用于 VB 中的可空类型

问题描述

我很惊讶在 C# 中:

int? a = null;
int? b = null;
intc = a == b; //c = true

在 VB 中:

Dim a As Integer? = Nothing
Dim b As Integer? = Nothing

if( a = b ) 生成异常,因为条件 a = b 显然被解析为 Nothing(而不是在 C# 中为 true)

我想我可以将我的条件更改为: if( (a Is Nothing AndAlso b Is Nothing) OrElse a = b) 但我觉得这很难看!有没有更优雅的方式来写这个如此简单的条件?

标签: vb.netoperatorsnullable

解决方案


推荐阅读