String.Equality (==) operator with example in C#

String.Equality (==) operator with example in C#

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

C# String.Equality administrator

==” is a String.Equality administrator in C#, it is utilized to check whether two strings objects have similar qualities or not.

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 worth. On the off chance that strings have a similar worth, it returns genuine, else it returns bogus.

Example:

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

    String.Equality:
    str1 == str2;

    Output:
    true

C# Example to think about two strings utilizing String.Equality (==) 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 have the same values");
            else
                Console.WriteLine("str1 and str2 don't 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 have the same values");
            else
                Console.WriteLine("str1 and str2 don't have the same values");

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

Output:

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

Recent Posts

error: Alert: Content is protected!!