Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

All of these

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What's the difference?

  • String.Empty and "" both represent a string of length zero.

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

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

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.IsNullOrEmpty to check for Nothing or 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 = "":

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

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

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

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.VisualBasic namespace (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:

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 vbNullString defined as Nothing instead of ""?

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

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


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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...