String.Inequality (!=) operator with example in C#

C# String.Inequality administrator: Here, we will find out about the String.Inequality (!=) administrator, its utilizations and so on.

C# String.Inequality administrator

“!=” is a String.Inequality administrator in C#, it is utilized to check whether two strings objects have similar values or not (returns genuine if strings don’t have similar values).

Syntax:

    public static bool operator != (string a, string b);

Parameter: It has two parameters both are strings to look at..

Return value: bool – it restores a Boolean value. In the event that strings have a similar value, it returns bogus, else it returns genuine.

Example:

    Input:
    string str1 = "JustTechReview";
    string str2 = "JustTechReview";

    String.Inequality:
    str1 != str2;

    Output:
    false

C# Example to think about two strings utilizing String.Inequality (!=) administrator:

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "JustTechReview";
            string str2 = "JustTechReview";

            //comparing strings
            Console.WriteLine("str1!=str2: " + (str1 != str2));
            if (str1 != str2)
                Console.WriteLine("str1 and str2 don't have the same values");
            else
                Console.WriteLine("str1 and str2 have the same values");

            str1 = "Hello world";
            str2 = "JustTechReview";

            //comparing strings
            Console.WriteLine("str1!=str2: " + (str1 != str2));
            if (str1 != str2)
                Console.WriteLine("str1 and str2 don't have the same values");
            else
                Console.WriteLine("str1 and str2 have the same values");

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output:

str1!=str2: False
str1 and str2 have the same values
str1!=str2: True
str1 and str2 don't have the same values

Leave a Comment

error: Alert: Content is protected!!