Empty string: Difference between revisions

Edited C# and Visual Basic .NET
(Edited C# and Visual Basic .NET)
Line 472:
static class Program
{
// TL;In DRshort:
public static void Foo()
{
Line 485:
if (s != null && s.Length == 0) { }
 
// Check for null or empty (mostmore idiomatic in .NET way):
if (string.IsNullOrEmpty(s)) { }
}
Line 492:
{
// Equality is somewhat convoluted in .NET.
// The meansmethods above are the author's recommendation for each case.
 
// s is initialized to null. It is a variable of the System.String type that is a null reference and is not
Line 498:
string s = null;
 
// Alias Console.WriteLine(bool) with a shorter name to make the demonstration code less verbose.
voidAction<bool> P(bool x) => Console.WriteLine(x);
 
// Assign the empty string literal to s.
Line 2,456:
End If
 
' Check for null or empty (mostmore idiomatic in .NET way):
If String.IsNullOrEmpty(s) Then
End If</lang>
Line 2,468:
 
Module Program
Sub Main()
' Equality is somewhat convoluted in .NET, and VB doesn't help by adding legacy means of comparison.
' The meansmethods above are the author's recommendation for each case.
' Some methods also return true if the string is Nothing/null; this is noted in the description for those that
' do.
 
Line 2,479 ⟶ 2,478:
Dim s As String = Nothing
 
' Alias Console.WriteLine(Boolean) with a shorter name to make the demonstration code less verbose.
Dim P =As SubAction(x AsOf Boolean) = AddressOf Console.WriteLine(x)
 
' Assign the empty string literal to s.
Line 2,578 ⟶ 2,577:
Console.WriteLine()
 
' Each of the meansmethods described above, except testing for a non-empty string.
P(s IsNot "")
P(Not Object.ReferenceEquals(s, ""))
Anonymous user