c# - 将 intResult 变暗为整数 = 无

标签 c# .net vb.net

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/

相关文章:

.net - NHibernate:覆盖 Equals 和 GetHashCode 的原因

c# - WPF 将集合的集合绑定(bind)到 DataGrid

c# - 为什么 .net 语言的性能会有所不同?

vb.net - 如何在 VB.NET 中将文件复制/替换到文件夹中?

.net - String.Replace 不会替换所有匹配项

c# - 如何在.net中实现和扩展Joshua的 builder 模式?

c# - 如何检查 Hashtable 值是否全为 0?

.net - 为什么我的 VB.Net 应用程序在编译时不断收到 "type is not defined"?

c# - 如何选择 Windows 窗体文本框中的所有文本?

c# - 检索两个列表的子列表的每种可能组合的算法