SoFunction
Updated on 2025-03-06

How to use and the difference between IsNullOrEmpty and IsNullOrWhiteSpace in C#

Preface

Today we will discuss two commonly used string processing methods in C#: IsNullOrEmpty and IsNullOrWhiteSpace. These two methods are very common when dealing with strings, but there are some slight differences between them. In this article, we will explain in detail the functions and usage scenarios of these two methods and help you better understand the differences between them.

IsNullOrEmpty

effect

This method is used to check if the string is null or empty string (""). Return true if the string is null or length is 0; otherwise return false. This method only focuses on the length of the string, regardless of the whitespace characters in it.

Source code implementation

    /// <summary>
    ///  Determine whether the string is null or an empty string    /// </summary>
    /// <param name="value">String</param>    /// &lt;returns&gt;&lt;/returns&gt;
    public static bool IsNullOrEmpty([NotNullWhen(false)] string? value)
    {
        return value == null ||  == 0;
    }

Example

    public static void Main(string[] args)
    {
        string str1 = null;
        string str2 = "";
        string str3 = " ";
        string str4 = "Time Chaser";
        (IsStringNullOrEmpty(str1));// Output: True        (IsStringNullOrEmpty(str2));// Output: True        (IsStringNullOrEmpty(str3));// Output: False        (IsStringNullOrEmpty(str4));// Output: False    }
    public static bool IsStringNullOrEmpty(string str)
    {
        return (str);
    }

IsNullOrWhiteSpace

effect

This method is used to check whether the string is null, an empty string ("") or contains only whitespace characters. Return true if the string is null, has a length of 0, or contains only whitespace characters (such as spaces, tabs, newlines); otherwise return false. Unlike IsNullOrEmpty, IsNullOrWhiteSpace takes into account whitespace characters in the string.

Source code implementation

    /// &lt;summary&gt;
    /// Is the string null, empty string or contains only whitespace characters [(whitespace characters such as spaces, tabs, line breaks, etc.)]    /// &lt;/summary&gt;
    /// <param name="value">String</param>    /// &lt;returns&gt;&lt;/returns&gt;
    public static bool IsNullOrWhiteSpace([NotNullWhen(false)] string? value)
    {
        if (value == null) return true;
        for (int i = 0; i &lt; ; i++)
        {
            //Judge whether each character is a whitespace character. If there is any non-whitespace character, that is, the character is not a whitespace character such as a space, a tab, or a newline.            //Then the condition !(value[i]) will be true, and false is immediately returned at this time, indicating that the string is not an empty string.            if (!(value[i])) return false;
        }
        return true;
    }

Example

    public static void Main(string[] args)
    {
        string str1 = null;
        string str2 = "";
        string str3 = " ";
        string str4 = "Time Chaser";
        (IsStringNullOrWhiteSpace(str1));// Output: True        (IsStringNullOrWhiteSpace(str2));// Output: True        (IsStringNullOrWhiteSpace(str3));// Output: True        (IsStringNullOrWhiteSpace(str4));// Output: False    }
    public static bool IsStringNullOrWhiteSpace(string str)
    {
        return (str);
    }

What is the difference between using IsNullOrEmpty and IsNullOrWhiteSpace in C#? That’s all for the article. For more related contents of using IsNullOrEmpty and IsNullOrWhiteSpace in C#, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!