How to use isnullorempty in C#?

In C#, IsNullOrEmpty is a static method used to determine if a string is null or empty. To use it, simply call the method directly on the string type. Below is an example of how to use it:

string str1 = null;
string str2 = "";
string str3 = "Hello World";

if (string.IsNullOrEmpty(str1))
{
    Console.WriteLine("str1 is null or empty");
}
else
{
    Console.WriteLine("str1 is not null or empty");
}

if (string.IsNullOrEmpty(str2))
{
    Console.WriteLine("str2 is null or empty");
}
else
{
    Console.WriteLine("str2 is not null or empty");
}

if (string.IsNullOrEmpty(str3))
{
    Console.WriteLine("str3 is null or empty");
}
else
{
    Console.WriteLine("str3 is not null or empty");
}

The output result is:

str1 is null or empty
str2 is null or empty
str3 is not null or empty

In the examples above, the IsNullOrEmpty method is used to check if str1, str2, and str3 are either null or empty strings, and then display the corresponding message based on the result of the check.

bannerAds