C# 与 VB.NET - 处理空结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/303502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:23:31  来源:igfitidea点击:

C# vs VB.NET - Handling of null Structures

c#vb.netnothing

提问by Hugoware

I ran across this and was wondering if someone could explain why this works in VB.NET when I would expect it should fail, just like it does in C#

我遇到了这个问题,想知道是否有人可以解释为什么这在 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

But then in VB.NET...

但是在 VB.NET 中...

Structure Person
    Public name As String
End Structure
...
Dim someone As Person = Nothing 'Wha? this is okay?

Is Nothing not the same as null (Nothing != null - LOL?), or is this just different ways of handling the same situation between the two languages?

Nothing 与 null ( Nothing != null - LOL?)是否不同,或者这只是处理两种语言之间相同情况的不同方式?

Why or what is handled differently between the two that makes this okay in one, but not the other?

为什么或两者之间的处理方式不同,使一个可以,但另一个不行?

[Update]

[更新]

Given some of the comments, I messed with this a bit more... it seems as if you actually have to use Nullable if you want to allow something to be null in VB.NET... so for example...

考虑到一些评论,我把这个搞得更糟了……如果你想在 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'

Too weird...

太诡异了...

采纳答案by BFree

If I remember correctly, 'Nothing' in VB means "the default value". For a value type, that's the default value, for a reference type, that would be null. Thus, assigning nothing to a struct, is no problem at all.

如果我没记错的话,VB 中的“Nothing”表示“默认值”。对于值类型,这是默认值,对于引用类型,这将是 null。因此,不给结构赋值完全没有问题。

回答by Jon Skeet

Nothingis roughly equivalent to default(T)for the relevant type. (Just checked, and this is true for strings as well - i.e. Nothingis a null reference in the context of strings.)

Nothing大致相当于default(T)相关类型。(刚刚检查过,这对于字符串也是如此 - 即Nothing在字符串的上下文中是一个空引用。)

回答by Dana

Also, structs are value types (much like int, char, etc.) and thus are non-nullable.

此外,结构体是值类型(很像 int、char 等),因此是不可为空的。

回答by Maxime Rouiller

I tried to search for it on MSDN but couldn't find anything relevant on the VB side. When searching for "struct" on C#, it clearly returns that it's a Value Type and can't be assigned null since... it's a value.

我试图在 MSDN 上搜索它,但在 VB 端找不到任何相关内容。在 C# 上搜索“struct”时,它清楚地返回它是一个值类型并且不能被赋值为 null 因为......它是一个值。

However, when looking on VB.NET keyword "Structure" it doesn't say "Value Type". Instead it says

但是,当查看 VB.NET 关键字“结构”时,它不会说“值类型”。相反它说

The Structure statement defines a composite value type that you can customize.

Structure 语句定义了可以自定义的复合值类型。

So... object?

所以……对象?

That would be my guess. I would like to have references to this behavior but couldn't find any.

那将是我的猜测。我想引用此行为,但找不到任何引用。

回答by piBoss

Because a Structure is made up of possibly several different Types (not a single value Type, but a possible composite of several different types), to ask if it is "Nothing" would break the logic of the use of "Nothing". Nothing tests differently depending on the type that you're testing and therefore a complex type does not adhere to the logic of the use of "Nothing". However, for this type of testing, i.e., with a structure having all of its component members at their respective "Nothing" values, we use the function "IsNothing". For example:

因为结构可能由几种不同的类型组成(不是单个值类型,而是几种不同类型的可能组合),所以询问它是否为“Nothing”会破坏使用“Nothing”的逻辑。没有根据您正在测试的类型进行不同的测试,因此复杂类型不符合使用“Nothing”的逻辑。然而,对于这种类型的测试,即结构的所有组件成员都处于各自的“Nothing”值,我们使用函数“IsNothing”。例如:

Public Class Employees
    Public Structure EmployeeInfoType
       Dim Name As String    ' String
       Dim Age as Integer    ' Integer
       Dim Salary as Single  ' Single
    End Structure

    Private MyEmp as New EmployeeInfoType

    Public Function IsEmployeeNothing(Employee As EmployeeInfoType) As Boolean
       If **IsNothing**(Employee) Then
          Return True
       Else
          Return False
       End If
    End Function
End Class