IsNullOrEmpty() Method in C#


The IsNullOrEmpty() method in C# is used to indicate whether the specified string is null or an empty string ("").

Syntax

The syntax is as follows−

public static bool IsNullOrEmpty (string val);

Above, the value val is the string to test.

Example

Let us now see an example −

 Live Demo

using System;
public class Demo {
   public static void Main(){
      string str1 = "Amit";
      string str2 = " ";
      Console.WriteLine("Is string1 null or empty? = "+string.IsNullOrEmpty(str1));
      Console.WriteLine("Is string2 null or empty? = "+string.IsNullOrEmpty(str2));
   }
}

Output

This will produce the following output −

Is string1 null or empty? = False
Is string2 null or empty? = False

Example

Let us now see another example −

 Live Demo

using System;
public class Demo {
   public static void Main(){
      string str1 = null;
      string str2 = String.Empty;
      Console.WriteLine("Is string1 null or empty? = "+string.IsNullOrEmpty(str1));
      Console.WriteLine("Is string2 null or empty? = "+string.IsNullOrEmpty(str2));
   }
}

Output

This will produce the following output −

Is string1 null or empty? = True
Is string2 null or empty? = True

Updated on: 03-Dec-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements