Partager via


String.IsNullOrEmpty Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: January 2011

Indicates whether the specified string is nulla null reference (Nothing in Visual Basic) or an Empty string.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function IsNullOrEmpty ( _
    value As String _
) As Boolean
public static bool IsNullOrEmpty(
    string value
)

Parameters

Return Value

Type: System.Boolean
true if the value parameter is nulla null reference (Nothing in Visual Basic) or an empty string (""); otherwise, false.

Remarks

IsNullOrEmpty is a convenience method that enables you to simultaneously test whether a String is nulla null reference (Nothing in Visual Basic) or its value is Empty. It is equivalent to the following code:

result = s Is Nothing OrElse s = String.Empty
result = s == null || s == String.Empty;

Examples

The following code example determines whether each of three strings has a value, is an empty string or is nulla null reference (Nothing in Visual Basic).

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim s1 As String = "abcd"
      Dim s2 As String = ""
      Dim s3 As String = Nothing

      outputBlock.Text += String.Format("String s1 {0}.", Test(s1)) & vbCrLf
      outputBlock.Text += String.Format("String s2 {0}.", Test(s2)) & vbCrLf
      outputBlock.Text += String.Format("String s3 {0}.", Test(s3)) & vbCrLf
   End Sub

   Public Shared Function Test(ByVal s As String) As String
      If String.IsNullOrEmpty(s) Then
         Return "is null or empty"
      Else
         Return String.Format("(""{0}"") is not null or empty", s)
      End If
   End Function 
End Class
' The example displays the following output:
'       String s1 ("abcd") is not null or empty.
'       String s2 is null or empty.
'       String s3 is null or empty.
using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string s1 = "abcd";
      string s2 = "";
      string s3 = null;

      outputBlock.Text += String.Format("String s1 {0}.", Test(s1)) + "\n";
      outputBlock.Text += String.Format("String s2 {0}.", Test(s2)) + "\n";
      outputBlock.Text += String.Format("String s3 {0}.", Test(s3)) + "\n";
   }

   public static String Test(string s)
   {
      if (String.IsNullOrEmpty(s))
         return "is null or empty";
      else
         return String.Format("(\"{0}\") is not null or empty", s);
   }
}
// The example displays the following output:
//       String s1 ("abcd") is not null or empty.
//       String s2 is null or empty.
//       String s3 is null or empty.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Change History

Date

History

Reason

January 2011

Expanded the Remarks section.

Customer feedback.