C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
IsNullOrEmpty: We use the IsNullOrEmpty method. This method correctly detects the empty string.
String.IsNullOrEmptyLength: Finally we access the Length property of the string. This returns, as expected, the value 0.
String LengthVB.NET program that uses String.Empty
Module Module1
Sub Main()
' Assign a string to String.Empty.
Dim value As String = String.Empty
' It equals an empty literal.
If value = "" Then
Console.WriteLine(1)
End If
' It is detected by IsNullOrEmpty.
If String.IsNullOrEmpty(value) Then
Console.WriteLine(2)
End If
' It has length 0.
Console.WriteLine(value.Length)
End Sub
End Module
Output
1
2
0
Tip: Passing String.Empty to a Function call is often more readable. But this comes down to your preference.
But: For almost all programs, no difference is going to be detected. For maximum performance, I recommend directly using the empty literal.