The String.Equals() method in C# is primarily used to compare two strings, it returns a boolean value true if the strings matches, and false if they don’t match.
In this blog, we’ll explore String.Equals() in-depth to help you make a correct decision in your C# programming.
Usage of String.Equals() in C#:
The String.Equals() is a method provided by the System.String class, it checks if two strings have the same content and returns a boolean value, it returns true if they are equal, false if they are not, here’s the basic syntax:
bool bStringEqual = string.Equals(inputStr1, inputStr2);
Here’s what each part does:
-
- inputStr1 is the first string you want to compare.
- inputStr2 is the second string you want to compare.
- bStringEqual is a boolean variable that stores the outcome of the comparison. It will be true if the strings are equal and false otherwise.
Example 1: Usage of string.Equals():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; namespace StringCompareMethodDemo { class Program { static void Main(string[] args) { string strInput1 = "Hello World"; string strInput2 = "Hello World"; bool areStringEqual = strInput1.Equals(strInput2); // Returns True Console.WriteLine("First Input String : {0}", strInput1); Console.WriteLine("Second Input String : {0}", strInput2); Console.WriteLine("Are String 1 equal to String 2 : {0}", areStringEqual); } } } |
In this case both of the string “Hello World” and “Hello World” considered as equal.

Key points of String.Equals() method:
Following are some key points of String.Equals() method.
-
- String.Equals() method performs a case-sensitive comparison by default. To perform a case-insensitive comparison, you must use the StringComparison enumeration as an argument:
- By default, String.Equals() considers cultural or linguistic differences while performing the comparison.
- It allows Null Safe comparisons between two stings.
Case-Sensitive Comparison of String.Equals():
By default, String.Equals() performs a case-sensitive comparison. This means that it considers the case of characters when determining equality. Let’s take an example.
Example 2: Case-Sensitive comparison of string.Equals() method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; namespace StringCompareMethodDemo { class Program { static void Main(string[] args) { string strInput1 = "Elephant"; string strInput2 = "elephant"; bool areStringEqual = strInput1.Equals(strInput2); // Returns false Console.WriteLine("First Input String : {0}", strInput1); Console.WriteLine("Second Input String : {0}", strInput2); Console.WriteLine("Are String 1 equal to String 2 : {0}", areStringEqual); } } } |
In this example, the variable
areStringEqual
will be false
because the comparison is case-sensitive, hence “Elephant” and “elephant” are treated as different strings.

However, to perform a case-insensitive comparison, we can use StringComparison.OrdinalIgnoreCase enumeration as an argument as follows.
Example 3: Case-insensitive comparison of string.Equals() method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; namespace StringCompareMethodDemo { class Program { static void Main(string[] args) { string strInput1 = "Elephant"; string strInput2 = "elephant"; bool areStringEqual = strInput1.Equals(strInput2, StringComparison.OrdinalIgnoreCase); // Returns false Console.WriteLine("First Input String : {0}", strInput1); Console.WriteLine("Second Input String : {0}", strInput2); Console.WriteLine("Are String 1 equal to String 2 : {0}", areStringEqual); } } } |
In this example,
- The output of
areStringEqual
will be True because we have used StringComparison.OrdinalIgnoreCase enumeration as an argument, which ignores the case-sensitive comparison, in this case both “Elephant” and “elephant” are treated as same strings.

Cultural Comparison using String.Equals():
String.Equals()
also considers cultural or linguistic differences by default. This means that it uses culture-specific rules for comparison. For instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; namespace StringCompareMethodDemo { class Program { static void Main(string[] args) { string strInput1 = "Hello World"; string strInput2 = "Héllo World"; bool areStringEqual = strInput1.Equals(strInput2); // Returns false Console.WriteLine("First Input String : {0}", strInput1); Console.WriteLine("Second Input String : {0}", strInput2); Console.WriteLine("Are String 1 equal to String 2 : {0}", areStringEqual); } } } |
In this case,
areStringEqual
will be false
because the default comparison considers the cultural difference between “Héllo World” (with an accent é) and “Hello World” (without an accent).

Null-Safe Comparison of String.Equals() in C#:
You can safely use String.Equal() to compare a string with another string which might be null , it doesn’t throw any null reference error while doing the comparison.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; namespace StringCompareMethodDemo { class Program { static void Main(string[] args) { string strInput1= "Hello World"; string strInput2 = null; bool areStringEqual = strInput1.Equals(strInput2); // Returns false Console.WriteLine("First Input String : {0}", strInput1); Console.WriteLine("Second Input String : {0}", strInput2); Console.WriteLine("Are String 1 equal to String 2 : {0}", areStringEqual); } } } |
Here,
String.Equals()
gracefully handles the comparison and returns false
when comparing one string with the other one which is null.
Conclusion:
In summary, you should choose the String.Equals()
method in C# when you need a simple, efficient, and customizable way to compare strings, whether it’s for exact case-sensitive comparisons, explicit case-insensitive checks, or null safe comparison.
Thanks for visiting 🙂 please look at some of other blogs on similar topics Usage of String.Split() method in C#