VB.NET 编译:
Dim intResult As Integer = Nothing
C# 不会:
int intResult = null; // cannot convert
结果最终如何进入MSIL?
不仅如此,VB代码还行:
If intResult > Nothing Then
编辑
好的,女士 says :
Assigning Nothing to a variable sets it to the default value for its declared type.
但它没有说明Nothing
比较。
最佳答案
Nothing
在 VB.NET 中实际上相当于 default(Type)
在 C# 中。
所以 int intResult = default(int);
是 C# 的等价物。
根据VB.NET Language Reference : "为变量分配 Nothing 会将其设置为其声明类型的默认值。如果该类型包含变量成员,则它们都设置为其默认值。"
编辑:关于与 Nothing
的比较:
我猜 intResult > Nothing
被解释为 intResult > 0
,因为 intResult
的编译时类型是Integer
,默认值为 0。如果 intResult 的类型在编译时未知(例如,它是一个盒装的 Integer
),我怀疑这不会编译。
参见 Community Content section在 VB.NET 语言引用页面的底部有一个类似的例子 (Nothing < 2
)。
关于c# - 将 intResult 变暗为整数 = 无,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4377336/