Computer Science

nullorempty whitespace


• IsNullOrEmpty: only null or empty string ""
• IsNullOrWhiteSpace: null, "", or just whitespace (space/tab/newline)

string.IsNullOrEmpty(string value)
                        Console.WriteLine(string.IsNullOrEmpty(null));   // true
Console.WriteLine(string.IsNullOrEmpty(""));     // true
Console.WriteLine(string.IsNullOrEmpty(" "));    // false
                    
                    

string.IsNullOrWhiteSpace(string value)
                        Console.WriteLine(string.IsNullOrWhiteSpace(null));   // true
Console.WriteLine(string.IsNullOrWhiteSpace(""));     // true
Console.WriteLine(string.IsNullOrWhiteSpace("   "));  // true
Console.WriteLine(string.IsNullOrWhiteSpace("\n"));   // true

                    
                    

Which one is preferred?

IsNullOrWhiteSpace is generally preferred in .NET Core because sometimes the user may only enter a space, which is generally considered “empty”.