String.IsNullOrEmpty() method with example in C#

C# String.IsNullOrEmpty() technique: Here, we will find out about the IsNullOrEmpty() strategy for String class with example.

C# String.IsNullOrEmpty() Method

String.IsNullOrEmpty() technique is a worked in strategy for String class and it is utilized to check whether a string is Null or Empty? On the off chance that string object isn’t introduced

with a right value it will be considered as “invalid string”, if string object is instated yet contains nothing, for example, it is doled out the value (“”) it will be considered as “empty string”.

Syntax:

    public static bool IsNullOrEmpty(String str);

The strategy is called with “string”/”String“. Here, “string” is a nom de plume of “String” class.

Parameter(s):

  • str – speaks to a string value or string item to be checked.
  • Return value: bool – it returns “Genuine” if str is invalid or unfilled, else it returns “False“.

Example:

    Input:
    string str1 = "";
    string str2 = null;
    string str3 = "JustTechReview";
    
    Function call
    Console.WriteLine(string.IsNullOrEmpty(str1));
    Console.WriteLine(string.IsNullOrEmpty(str2));
    Console.WriteLine(string.IsNullOrEmpty(str3));

    Output:
    True
    True
    False

C# Example to change over the string to characters exhibit utilizing String.IsNullOrEmpty() strategy:

Example 1:

using System;
class JustTechReview
{
    static void Main()
    {
        // declaring string variables
        string str1 = "";
        string str2 = null;
        string str3 = "JustTechReview";

        // check whether string is empty/null or not
        Console.WriteLine(string.IsNullOrEmpty(str1));
        Console.WriteLine(string.IsNullOrEmpty(str2));
        Console.WriteLine(string.IsNullOrEmpty(str3));
    }
}

Output:

True
True
False

Example 2:

using System;

class JustTechReview
{
    static void Main()
    {
        // declaring string variable
        string str = "JustTechReview";

        // checking whether string is null/empty or not
        if(string.IsNullOrEmpty(str))
            Console.WriteLine("str is empty or null");
        else
            Console.WriteLine("str is not empty or null");

        //now assigning null to the string
        str = null;

        // checking whether string is null/empty or not
        if(string.IsNullOrEmpty(str))
            Console.WriteLine("str is empty or null");
        else
            Console.WriteLine("str is not empty or null");

    }
}

Output:

str is not empty or null
str is empty or null

Leave a Comment

error: Alert: Content is protected!!