How to check if string is null or empty? or it does not contains anything in C#

In every project developed within the .NET framework, whether it's a web, console, Windows, or mobile application, it is crucial to validate whether a string is empty, null, or contains anything, including whitespace. Therefore, I'm sharing this C# tip to demonstrate the optimal approach for checking if a string is null, empty, or consists solely of blank space.

Certainly! In C#, you can efficiently check if a string is null, empty, or contains only whitespace using the string.IsNullOrWhiteSpace method. This method returns true if the string is null, empty, or consists only of white-space characters; otherwise, it returns false.

Null Or Empty

The term "white space" includes all characters that are not visible on screen. For example, space, line break, tab and empty string are white space characters.

Lets take example. Create a new console application in Visual Studio. Try this code below.

class Program
{
    static void Main(string[] args)
    {
        string nullString = null;
        string emptyString = "";
        string whitespaceString = "    ";
        string nonEmptyString = "Mudmatter";

        Console.WriteLine("Result by using checking double quotes method - Wrong Method");
        Console.WriteLine("{{nullstring}} is null or empty? {0}", nullString == "");
        Console.WriteLine("{{emptyString}} is null or empty? {0}", emptyString == "");
        Console.WriteLine("{{whitespaceString}} is null or empty? {0}", whitespaceString == "");
        Console.WriteLine("{{nonEmptyString}} is null or empty? {0}", nonEmptyString == "");
        Console.WriteLine("");
        Console.WriteLine("---------------------------------------");
        Console.WriteLine("");

        Console.WriteLine("Result by using IsNullOrEmpty method - OK but not good");
        Console.WriteLine("{{nullstring}} is null or empty? {0}", string.IsNullOrEmpty(nullString));
        Console.WriteLine("{{emptyString}} is null or empty? {0}", string.IsNullOrEmpty(emptyString));
        Console.WriteLine("{{whitespaceString}} is null or empty? {0}", string.IsNullOrEmpty(whitespaceString));
        Console.WriteLine("{{nonEmptyString}} is null or empty? {0}", string.IsNullOrEmpty(nonEmptyString));
        Console.WriteLine("");
        Console.WriteLine("----------------------------------------");
        Console.WriteLine("");

        Console.WriteLine("Result by using IsNullOrWhiteSpace method - Best method");
        Console.WriteLine("{{nullstring}} is null or empty? {0}", string.IsNullOrWhiteSpace(nullString));
        Console.WriteLine("{{emptyString}} is null or empty? {0}", string.IsNullOrWhiteSpace(emptyString));
        Console.WriteLine("{{whitespaceString}} is null or empty? {0}", string.IsNullOrWhiteSpace(whitespaceString));
        Console.WriteLine("{{nonEmptyString}} is null or empty? {0}", string.IsNullOrWhiteSpace(nonEmptyString));

        Console.ReadKey();
    }
}

 

Output:

Is null or whitespace program output

 

Short Video

Conclusion

There are various methods to check if string is empty or not but the best method is using the string.IsNullOrWhiteSpace method. This method can check empty, null & any white space characters which are not visible on screen. So I recommend to use the string.IsNullOrWhiteSpace method.

 



If you find anything inappropriate please report it here.

Leave a Reply

Please login to post comments.

Top