VB.NET编译:
Dim intResult As Integer = Nothing
Run Code Online (Sandbox Code Playgroud)
C#没有:
int intResult = null; // cannot convert
Run Code Online (Sandbox Code Playgroud)
结果最终如何转向MSIL?
比VB代码还好:
If intResult > Nothing Then
Run Code Online (Sandbox Code Playgroud)
编辑
好的,MS 说:
为变量赋值Nothing将其设置为其声明类型的默认值.
但它没有说明Nothing 比较.
Nothing在VB.NET中实际上相当于default(Type)在C#中.
那么int intResult = default(int);C#是等价的.
根据VB.NET语言参考:"为变量赋值,将其设置为其声明类型的默认值.如果该类型包含变量成员,则它们都被设置为默认值."
编辑:关于对比Nothing:我猜intResult > Nothing被解释为intResult > 0,由于编译时类型intResult是Integer,其中有一个默认值为0.如果intResult的类型没有在编译时(如已知的,它是一个盒装Integer),我怀疑这不会编译.
有关类似示例(),请参阅" VB.NET语言参考"页面底部的" 社区内容"部分Nothing < 2.
| 归档时间: |
|
| 查看次数: |
897 次 |
| 最近记录: |