C# string.IsNullOrEmpty, IsNullOrWhiteSpace

C# string.IsNullOrEmpty, IsNullOrWhiteSpace

In this article how to check string IsNullOrEmpty, IsNullOrWhiteSpace. The string.IsNullOrEmpty method tests strings. It checks for string references that are null or have no data. IsNullOrWhiteSpace tests for whitespace.

 

using System;

class usingcsharp
{
    static void Main()
    {
         string s = null;
        if (string.IsNullOrEmpty(s) == true)
        {
            // True.
            Console.WriteLine("Null or empty");
        }
        else
        {
            Console.WriteLine("Not null and not empty");
        }

         string y = "";
        if (string.IsNullOrEmpty(y) == true)
        {
            // True.
            Console.WriteLine("Null or empty");
        }
        else
        {
            Console.WriteLine("Not null and not empty");
        }

        string z = "sam";
        if (string.IsNullOrEmpty(z))
        {
            // False.
            Console.WriteLine("Null or empty");
        }
        else
        {
            Console.WriteLine("Not null and not empty");
        }
    }
}

2021-10-03