Home » C#.Net

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

C# String.Equality operator: Here, we are going to learn about the String.Equality (==) operator, its usages etc.
Submitted by IncludeHelp, on March 18, 2019

C# String.Equality operator

"==" is a String.Equality operator in C#, it is used to check whether two strings objects have the same values or not.

Syntax:

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

Parameter: It has two parameters both are strings to compare..

Return value: bool – it returns a Boolean value. If strings have the same value, it returns true, else it returns false.

Example:

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

    String.Equality:
    str1 == str2;

    Output:
    true

C# Example to compare two strings using String.Equality (==) operator

using System;
using System.Text;

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

            //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 = "IncludeHelp";

            //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

Reference: String.Equality(String, String) Operator






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.