Home »
C# Tutorial
C# String.IsNullOrEmpty() Method with Example
In this tutorial, we will learn about the String.IsNullOrEmpty() method with its usage, syntax, and examples.
By IncludeHelp Last updated : April 07, 2023
C# String.IsNullOrEmpty() Method
The String.IsNullOrEmpty() method is a built-in method of String class and it is used to check whether a string is Null or Empty? If string object is not initialized with a correct value it will be considered as "null string", if string object is initialized but contains nothing i.e. it is assigned the value ("") it will be considered as "empty string".
Syntax
public static bool IsNullOrEmpty(String str);
The method is called with "string"/ "String". Here, "string" is an alias of "String" class.
Parameter(s)
- str – represents a string value or string object to be checked.
Return Value
bool – it returns "True" if str is null or empty, otherwise it returns "False".
Input/Output Example
Input:
string str1 = "";
string str2 = null;
string str3 = "IncludeHelp";
Function call
Console.WriteLine(string.IsNullOrEmpty(str1));
Console.WriteLine(string.IsNullOrEmpty(str2));
Console.WriteLine(string.IsNullOrEmpty(str3));
Output:
True
True
False
C# String.IsNullOrEmpty() Method Example 1
using System;
class IncludeHelp {
static void Main() {
// declaring string variables
string str1 = "";
string str2 = null;
string str3 = "IncludeHelp";
// check whether string is empty/null or not
Console.WriteLine(string.IsNullOrEmpty(str1));
Console.WriteLine(string.IsNullOrEmpty(str2));
Console.WriteLine(string.IsNullOrEmpty(str3));
}
}
Output
True
True
False
C# String.IsNullOrEmpty() Method Example 2
using System;
class IncludeHelp {
static void Main() {
// declaring string variable
string str = "IncludeHelp";
// checking whether string is null/empty or not
if (string.IsNullOrEmpty(str))
Console.WriteLine("str is empty or null");
else
Console.WriteLine("str is not empty or null");
//now assigning null to the string
str = null;
// checking whether string is null/empty or not
if (string.IsNullOrEmpty(str))
Console.WriteLine("str is empty or null");
else
Console.WriteLine("str is not empty or null");
}
}
Output
str is not empty or null
str is empty or null