我遇到了这个问题,想知道是否有人可以解释为什么它在 VB.NET 中有效,而我认为它应该会失败,就像在 C# 中一样
//The C# Version
struct Person {
public string name;
}
...
Person someone = null; //Nope! Can't do that!!
Person? someoneElse = null; //No problem, just like expected
但是在 VB.NET 中...
Structure Person
Public name As String
End Structure
...
Dim someone As Person = Nothing 'Wha? this is okay?
Nothing 与 null ( Nothing != null - LOL?)是否不同,或者这只是两种语言之间处理相同情况的不同方式?
为什么或两者之间的处理方式有所不同,这使得这在一个中可以解决,而在另一个中则不行?
[更新]
鉴于一些评论,我对此更加混乱......似乎如果你想在VB.NET中允许某些东西为空,你实际上必须使用Nullable......所以例如......
'This is false - It is still a person'
Dim someone As Person = Nothing
Dim isSomeoneNull As Boolean = someone.Equals(Nothing) 'false'
'This is true - the result is actually nullable now'
Dim someoneElse As Nullable(Of Person) = Nothing
Dim isSomeoneElseNull As Boolean = someoneElse.Equals(Nothing) 'true'
太诡异了……