C# String IsNullOrEmpty

Description

String IsNullOrEmpty indicates whether the specified string is null or an Empty string.

Syntax

String.IsNullOrEmpty has the following syntax.


public static bool IsNullOrEmpty(
  string value
)

Parameters

String.IsNullOrEmpty has the following parameters.

  • value - The string to test.

Returns

String.IsNullOrEmpty method returns true if the value parameter is null or an empty string (""); otherwise, false.

Example

The following example examines three strings and determines whether each string has a value, is an empty string, or is null.


using System;/*from w  ww  .j a v a  2  s . co  m*/

class Sample 
{
    public static void Main() 
    {
        string s1 = "abcd";
        string s2 = "";
        string s3 = null;
    
        Console.WriteLine("String s1 {0}.", Test(s1));
        Console.WriteLine("String s2 {0}.", Test(s2));
        Console.WriteLine("String s3 {0}.", Test(s3));
    }

    public static String Test(string s)
    {
        if (String.IsNullOrEmpty(s)) 
            return "is null or empty";
        else 
            return String.Format("(\"{0}\") is neither null nor empty", s);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version