C#VS VB.NET - 空结构的处理 [英] C# vs VB.NET - Handling of null Structures

查看:168
本文介绍了C#VS VB.NET - 空结构的处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跑过这一点,并想知道,如果有人可以解释为什么这工作在VB.NET的时候我希望它失败了,就像它在C#中确实

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#

//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 ...

But then in VB.NET...

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



是没有什么不一样,空(没什么= NULL - !?LOL),或者这只是不同的处理两种语言之间的相同的情况如何?

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?

为什么或什么是两个,使这个好一间不同的处理,而不是其他?

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

[更新]

由于一些评论,我搞砸这个多一点......它好像你确实有,如果你想允许的东西是在VB.NET空使用可空...所以...例如

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...

'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'

太诡异......

推荐答案

如果我没有记错,在VB中无的意思是默认值。对于一个值类型,这就是默认值,引用类型,这将是空。因此,在结构分配什么,是一点问题都没有。

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.

这篇关于C#VS VB.NET - 空结构的处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆