vb.net vbNullString、String.Empty 和 "" 之间有什么区别?

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

What is the difference between vbNullString, String.Empty and ""?

vb.netstring-comparison

提问by Dominic B.

All of these

所有这些

  • txtUsername.Text <> vbNullString
  • txtUsername.Text <> String.Empty
  • txtUsername.Text <> ""
  • txtUsername.Text <> vbNullString
  • txtUsername.Text <> String.Empty
  • txtUsername.Text <> ""

seem to return the same result. So, what's the difference between vbNullString, String.Emptyand ""?

似乎返回相同的结果。那么,vbNullString,String.Empty和之间有什么区别""

回答by Heinzi

What's the difference?

有什么不同?

  • String.Emptyand ""both represent a string of length zero.

  • Contrary to the documentation, which claims that vbNullString"[r]epresents a zero-length string", vbNullStringis actually Nothing(nullin C#, see Reference Source).

  • String.Empty并且""都代表一个长度为零的字符串。

  • 与声称“ [r] 表示零长度字符串的文档相反,实际上是(在 C# 中,请参阅参考源)。vbNullStringvbNullStringNothingnull

(Note that this only applies to VB.NET! The situation is different in VBA, see below for details.)

(请注意,这只适用于 VB.NET!在 VBA 中情况有所不同,详见下文。)

So how do I check if a string is null/Nothing or empty?

那么如何检查字符串是否为空/无或空?

Contrary to what the other answer says, you do not need to use String.IsNullOrEmptyto check for Nothingor an empty string, since the =operator in Visual Basic treats Nothing and an empty string as equivalent. Thus, the idiomatic way to check for "null or empty string" in VB.NET is to simply use = "":

与其他答案所说的相反,您不需要使用String.IsNullOrEmptyto 检查Nothing或空字符串,因为=Visual Basic 中的运算符将 Nothing 和空字符串视为等效的。因此,在 VB.NET 中检查“null 或空字符串”的惯用方法是简单地使用= ""

Dim s As String = Nothing
If s = "" Then Console.WriteLine("YES")     ' Prints YES

Thus, Not String.IsNullOrEmpty(s), s <> "", s <> String.Emptyand s <> vbNullStringare all equivalent. I prefer the concise s <> "", but it's mainly a question of preference and style.

因此,Not String.IsNullOrEmpty(s)s <> ""s <> String.Emptys <> vbNullString都是等价的。我更喜欢简洁的s <> "",但这主要是偏好和风格的问题。

Does that mean that I can use an empty string and Nothing interchangeably?

这是否意味着我可以交替使用空字符串和 Nothing ?

In general: no. This equivalence of an empty string and Nothing is only true for VB.NET's built-in =and <>operators as well as most of the methods in the Microsoft.VisualBasicnamespace (Len(...), Trim(...), ...). Once you go into pure .NET territory (for example, by using methods from the rest of the Base Class Library), Nothing and the empty string are treated differently:

一般来说:没有。空字符串和 Nothing 的这种等价性仅适用于 VB.NET 的内置=<>运算符以及Microsoft.VisualBasic命名空间(Len(...), Trim(...), ...)中的大多数方法。一旦进入纯 .NET 领域(例如,通过使用来自基类库其余部分的方法),Nothing 和空字符串的处理方式不同:

Dim sNothing As String = Nothing
Dim sEmpty As String = ""

Console.WriteLine(sEmpty = sNothing)                ' True
Console.WriteLine(sEmpty.Equals(sNothing))          ' False
Console.WriteLine(String.Equals(sEmpty, sNothing))  ' False

Console.WriteLine(Len(sEmpty))                      ' 0
Console.WriteLine(Len(sNothing))                    ' 0
Console.WriteLine(sEmpty.Length)                    ' 0
Console.WriteLine(sNothing.Length)                  ' throws a NullReferenceException

Why is vbNullStringdefined as Nothinginstead of ""?

为什么vbNullString定义为Nothing而不是""

Backwards compatibility. In VBA (and "VB Classic"), vbNullStringcould be used to get a "null string reference"1. Within VBA, vbNullStringwas treated like the empty string "", but when interacting with non-VB-code, vbNullStringwas mapped to the NULL pointer and ""was mapped to an empty string.

向后兼容性。在 VBA(和“VB Classic”)中,vbNullString可用于获取“空字符串引用”1。在 VBA 中,vbNullString被视为空字符串"",但在与非 VB 代码交互时,vbNullString被映射到 NULL 指针""并被映射到空字符串

When using PInvoke in VB.NET, Nothingis mapped to the NULL pointer and ""is mapped to an empty string, so it makes sense to have the legacy constant vbNullStringequal Nothingin VB.NET.

在 VB.NET 中使用 PInvoke 时,Nothing映射到 NULL 指针并""映射到空字符串,因此在 VB.NET 中使用vbNullString相同的旧常量是有意义的Nothing



1 which is completely different from VBA's Nulland Empty, which are special Variantsubtypes, and VBA's Nothing, which, in VBA, only applies to objects and not to strings.

1NullEmpty作为特殊Variant子类型的 VBA 和 和 VBA完全不同Nothing,后者在 VBA 中仅适用于对象而不适用于字符串。

回答by Dominic B.

vbNullString is a constant, more likely out of VB6, String.Emptyand ""are the same, some say that there is a performance difference, but that is not true, it is your choice what you use.

vbNullString 是一个常量,更可能是 VB6 出来的,String.Empty而且""是一样的,有人说性能上有差异,但事实并非如此,使用什么是你的选择。

To check whether a string is empty you can use If String.IsNullOrEmpty(String). The advantage is that it will also check for null, because string is a class and because of this a reference type.

要检查字符串是否为空,您可以使用If String.IsNullOrEmpty(String). 优点是它还会检查null,因为 string 是一个类,因此它是一个引用类型。

回答by Mayur Bari

If are going to insert values to MS-SQL database and if column doesn't allows null(Not null) then strValue=vbNullString will generate exception but strValue="" will insert blank spaces.

如果要将值插入 MS-SQL 数据库,并且列不允许为空(非空),则 strValue=vbNullString 将生成异常,但 strValue="" 将插入空格。