空(在 C# 中)与无(在 vb.net 中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44192049/
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-09-17 20:17:18  来源:igfitidea点击:

Null(In C#) Vs Nothing(in vb.net)

c#vb.net

提问by Reddy

How is C# NULL different from vb.net Nothing?

C# NULL 与 vb.net Nothing 有何不同?

Console.WriteLine(Nothing = "") => True

vs

对比

Console.WriteLine(null==""); => False

My understanding was that both nulland Nothingare same. But above code clearly explains it is not.

我的理解是,这两个nullNothing是一样的。但上面的代码清楚地解释了它不是。

What is the equivalent of C# nullin VB.NET?

nullVB.NET中 C# 的等价物是什么?

采纳答案by SSS

In your code, VB guesses that you are comparing Strings, since one of the operands is a String. In Stringcomparisons, Nothingis equivalent to the empty String"". It then does a value comparison, which returns True.

在您的代码中,VB 猜测您正在比较Strings,因为其中一个操作数是String. 在String比较中,Nothing相当于空的String""。然后进行值比较,返回True.

Use Isto compare references:

使用Is比较引用:

Console.WriteLine(Nothing Is "") '=> False

回答by Fabio

You have chosen little bid complicated example to test difference between Nothingin vb.net and nullin c#

您选择了小标复杂示例来测试Nothingvb.net 和nullc# 中的差异

From Visual basic language specification about Nothing:

来自 Visual Basic 语言规范关于Nothing

Nothing is a special literal; it does not have a type and is convertible to all types in the type system, including type parameters. When converted to a particular type, it is the equivalent of the default value of that type.

没有什么是特殊的文字;它没有类型并且可以转换为类型系统中的所有类型,包括类型参数。当转换为特定类型时,它相当于该类型的默认值

If you read description of default value expressionsfrom Microsoft documentation you will notice that Nothingin vb.net and default(T)in C# have similar behaviour.

如果您阅读Microsoft 文档中对默认值表达式的描述,您会注意到Nothing在 vb.net 和default(T)C# 中具有相似的行为。

For example

例如

Dim isSomething As Boolean = Nothing ' = False
Dim amount As Integer = Nothing ' = 0
Dim value As String = Nothing ' = null (notice not empty string!)

As prove that default value for string is not empty string, as many of comments/answers here stays

证明字符串的默认值不是空字符串,因为这里保留了许多评论/答案

Dim value As String = Nothing
If value.Equals("") Then ' will throw NullReferenceException

End If 

Comparing Nothingto empty string ""with "="operator is special case for vb.net, because Nothingfor string will evaluate to null

用运算符比较Nothing空字符串是 vb.net 的特殊情况,因为字符串将评估为"""="Nothingnull

From Visual basic language specification about relational operators for type String:

来自关于类型关系运算符的 Visual Basic 语言规范String

The operators return the result of comparing the two values using either a binary comparison or a text comparison. The comparison used is determined by the compilation environment and the Option Compare statement. A binary comparison determines whether the numeric Unicode value of each character in each string is the same. A text comparison does a Unicode text comparison based on the current culture in use on the .NET Framework. When doing a string comparison, a null reference is equivalent to the string literal "".

运算符使用二进制比较或文本比较返回比较两个值的结果。使用的比较由编译环境和 Option Compare 语句决定。二进制比较确定每个字符串中每个字符的数字 Unicode 值是否相同。文本比较基于 .NET Framework 上使用的当前区域性进行 Unicode 文本比较。进行字符串比较时,空引用等效于字符串文字 ""

For checking string equality in vb.net you should use String.Equalsmethod or Isoperator.

要检查 vb.net 中的字符串相等性,您应该使用String.Equals方法或Is运算符。

Based on above language specifications

基于上述语言规范

My understanding was that both null and Nothing are same. But above code clearly explains it is not.

我的理解是 null 和 Nothing 都是一样的。但上面的代码清楚地解释了它不是。

They are not same, Nothingin vb.net is equivalent to default(T)in C#

它们不一样,Nothing在 vb.net 中相当于default(T)在 C# 中

What is the equivalent of C# null in VB.NET?

VB.NET 中 C# null 的等价物是什么?

In C# you cannot set nullto value types, so for reference types (String) equivalent for C# nullin vb.net will be Nothing.
For example (null == default(String))

在 C# 中,您不能设置null值类型,因此对于引用类型 ( String)null在 vb.net 中等效于 C#将是Nothing.
例如(null == default(String))

回答by Racil Hilan

The final version of the accepted answer correctly answers the question. However, Fabio added some useful information to his answer, but it has a number of incorrect information and concepts. So this answer is an attempt to merge the two answers in a more complete one with the correct information.

接受答案的最终版本正确回答了问题。但是,Fabio 在他的回答中添加了一些有用的信息,但它有许多不正确的信息和概念。所以这个答案是尝试将两个答案合并为一个更完整的具有正确信息的答案。

First of all, Nothingin VB is equivalent to nullin C#, so your understanding of that is correct, at least partially (because that's only one use of the Nothingkeyword). It's been reflected throughout the MSDN literature, although it's been fading recently and being replaced with the word nullin many comments. Your confusion arises from the behavior and features of VB which differ from C# and programmers who are coming from other languages and not experienced in VB get confused by the results.

首先,Nothing在 VB 中等同于null在 C# 中,因此您对此的理解是正确的,至少是部分正确的(因为这只是Nothing关键字的一种用法)。它已经反映在整个 MSDN 文献中,尽管它最近逐渐消失并null在许多评论中被这个词取代。您的困惑源于 VB 与 C# 不同的行为和特性,而来自其他语言且没有 VB 经验的程序员会对结果感到困惑。

As stated in the accepted answer, the question is considered a string comparison by VB because one of the operands is a string. In string comparisons, VB treats Nothingthe same as the empty string ""as per MSDN:

正如接受的答案中所述,该问题被 VB 视为字符串比较,因为其中一个操作数是字符串。在字符串比较,VB对待Nothing同为空字符串""按照MSDN

Numeric comparisons treat Nothing as 0. String comparisons treat Nothing as "" (an empty string).

数字比较将 Nothing 视为 0。字符串比较将 Nothing 视为 ""(空字符串)。

Now, why is that happening in VB? It happens because VB uses the Nothingkeyword for two different things (similar to how it uses the =operator for two different things, assignment and comparison). The first usage is equivalent to nullin C# as I mentioned above. The second usage is equivalent to the default(T)operator in C#. Like with the =operator, VB knows which way to use Nothingdepending on the context. Examples:

现在,为什么会在 VB 中发生这种情况?发生这种情况是因为 VB 将Nothing关键字用于两种不同的事情(类似于它如何将=运算符用于两种不同的事情,赋值和比较)。第一种用法等同null于我上面提到的 C# 中的用法。第二种用法相当于default(T)C# 中的运算符。与=操作符一样,VB 知道Nothing根据上下文使用哪种方式。例子:

In this line, VB uses Nothingas null:

在这一行中,VBNothing用作null

Console.WriteLine(s Is Nothing) //C#: (s == null)

In these lines, VB uses Nothingas default(T)to assign the default value to the variable:

在这些行中,VB 使用Nothingasdefault(T)将默认值分配给变量:

Dim s As String = Nothing //C#: string s = default(string);
Dim i As Integer = Nothing //C#: int i = default(int);

Finally, when comparing variables of different types, both VB and C# convert one operand to match the type of the other operand. For example, if you compare double and integer values. Both VB and C# will return truefor this line:

最后,当比较不同类型的变量时,VB 和 C# 都会转换一个操作数以匹配另一个操作数的类型。例如,如果您比较双精度值和整数值。VB 和 C# 都将返回true这一行:

Console.WriteLine(5.0 = 5) //C#: (5.0 == 5)

However, VB and C# differ in what types can be converted to what. Also, the empty string ""and nullare treated the same in string comparisons in VB, but not in C#. This makes things easier in most cases, which is something known about VB in general. So you can easily test if your string has no value like this:

但是,VB 和 C# 在哪些类型可以转换为哪些类型方面有所不同。此外,空字符串""null在 VB 中的字符串比较中被视为相同,但在 C# 中则不同。这在大多数情况下使事情变得更容易,这通常是关于 VB 的。所以你可以很容易地测试你的字符串是否没有这样的值:

Console.WriteLine(s = Nothing)
Console.WriteLine(s = "")

Both lines will return the same result because VB treats Nothingas ""for comparison. In C#, you cannot do that and you have to test for both like this:

两行都将返回相同的结果,因为 VB 将其Nothing视为""比较。在 C# 中,你不能这样做,你必须像这样测试两者:

Console.WriteLine(s == null && s == "")

Obviously, VB is easier and shorter than C# in this case which is the most common one. However, this comes at the cost of losing some control. In the less common cases when you only want to test for null reference, C# become obviously better because you can still use the ==operator:

显然,在这种情况下,VB 比 C# 更容易和更短,这是最常见的一种。然而,这是以失去一些控制为代价的。在不太常见的情况下,当您只想测试空引用时,C# 显然变得更好,因为您仍然可以使用==运算符:

Console.WriteLine(s == null)

While in VB, you cannot use the =operator and you have to use another one, the Isoperator:

而在 VB 中,您不能使用=运算符,而必须使用另一个Is运算符,即:

Console.WriteLine(s Is Nothing)

Also although VB is easier in the previous point, C# is clearer. That's why from code clarity perspective, this line can be used instead in both languages with the same result:

此外,虽然 VB 在上一点中更容易,但 C# 更清晰。这就是为什么从代码清晰的角度来看,这一行可以在两种语言中使用,结果相同:

Console.WriteLine(string.IsNullOrEmpty(s))

回答by Mad Myche

Nothingin VB varies by the objects type: - Reference types equate to null- Value types equate to the default value; which for a nullable type is null

Nothing在 VB 中因对象类型而异: - 引用类型等同于null- 值类型等同于默认值;对于可空类型,它是空的

回答by Ivanzinho

Microsoft referenceis right, it says

微软的参考是正确的,它说

Represents the default value of any data type.

代表任何数据类型的默认值。

But it wasn't clear enough to me until I created a VB.NET console app with following code:

但是直到我使用以下代码创建了一个 VB.NET 控制台应用程序之前,我还不够清楚:

    Dim myText As String = Nothing
    Dim myInt As Integer = Nothing
    Dim myFloat As Single = Nothing
    Dim myBoolean As Boolean = Nothing
    Dim myDate As DateTime = Nothing

decompiled it using ILSpy and the C# resulting code was:

使用 ILSpy 反编译它,C# 生成的代码是:

    string myText = null;
    int myInt = 0;
    float myFloat = 0f;
    bool myBoolean = false;
    DateTime myDate = DateTime.MinValue;

There is only Nothingliteral to specify a null value in VB.NET.

Nothing在 VB.NET 中只有文字可以指定空值。

From C# default(Type)is equivalent to VB Nothing

从 C#default(Type)相当于 VBNothing