C# String Equals Method

  Keywords : string equals in c#, c# equals example, c# object equals, csharp string equals ignore case example, c# string equals function example, csharp string equals example

Here we will learn string equals method in c# with examples and how to use c# string equals method to check whether the specified strings are equal or not by ignoring case or case insensitive operation with examples.

C# String Equals Method

In c#, the string Equals method is used to check whether the specified two string objects is having same value or not. If both string object values are equal, then the Equals() method will return true otherwise false.

 

In case, if both string objects is having null value, then the string Equals() method will return true.

 

Following is the pictorial representation of using string Equals() method to check whether the given string objects are equal or not in c# programming language.

 

C# String Equals Method Representation Diagram

 

If you observe above diagram, we defined a two strings “Suresh”, “Dasari” and checking whether the both strings are equal or not using Equals method.

C# String Equals Method Syntax

Following is the syntax of defining a string Equals method to check whether the given string objects are equal or not in c# programming language.

 

public bool Equals(string a, string b)

If you observe syntax, we will use both defined strings to check whether both the strings are equal or not using Equals method.

C# String Equals Method Example

Following is the example of using string Equals() method to check whether the given strings are equal or not in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            string fname = "Suresh";

            string lname = "Dasari";

            Console.WriteLine("{0} Equals to {1}? : {2}", fname, lname, fname.Equals(lname));

            string l_name = "suresh";

            Console.WriteLine("{0} Equals to {1}? : {2}", fname, l_name, fname.Equals(l_name));

            string u_name = "Suresh";

            Console.WriteLine("{0} Equals to {1}? : {2}", fname, u_name, fname.Equals(u_name));

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we used a string Equals() method to check whether the specified string values are equal or not.

 

When we execute above c# program, we will get the result like as shown below.

 

C# String Equals Method Example Result

 

If you observe above result, the string Equals() method checking the specified string values are equal or not and returned a boolean value.

C# String Equals Ignore Case

Generally, in c# the string Equals() method will perform a case-sensitive string comparison. In case, if we want to perform case insensitive string comparison, then we need to use OrdinalIgnoreCase property along with Equals method.

 

Following is the example of ignoring the case while comparing the strings in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            string fname = "Suresh";

            string l_name = "suresh";

            Console.WriteLine("{0} Equals to {1}? : {2}", fname, l_name, fname.Equals(l_name, StringComparison.OrdinalIgnoreCase));

            string u_name = "Suresh";

            Console.WriteLine("{0} Equals to {1}? : {2}", fname, u_name, fname.Equals(u_name));

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we used a “OrdinalIgnoreCase” property with Equals() method to perform case insensitive comparison by ignoring the case of characters in c# programming language.

 

When we execute above c# program, we will get the result like as shown below.

 

C# String Equals Ignore Case Example Result

 

If you observe above result, the string Equals() method has return a True for both “Suresh” and “suresh” values by ignoring the case.

 

This is how we can use Equals() method to check whether the specified string object values are equal or not based on our requirements.