C# String.Equals method is primarily used to compare two strings, it returns a boolean value as true if the strings matches, and false if they don’t match. In this blog, we’ll explore String.Equals method in-depth to make a correct decision in C# programming.
Syntax of String.Equals() in C#:
In C#, the String.Equals method is provided by the System.String class, here’s the basic syntax.
bool bStringEqual = string.Equals(inputStr1, inputStr2);
Here’s what each part does:
-
- inputStr1 is the first string and inputStr2 is the second string you want to compare with.
- bStringEqual is a boolean variable that stores the outcome of the comparison. ( true if the strings are equal and false otherwise)
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, you must use StringComparison enumeration as an argument to perform a case-insensitive comparison.
- String.Equals() considers cultural or linguistic differences while performing the comparison.
- It allows Null Safe comparisons between two stings.
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.

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.

In order to perform a case-insensitive comparison, we must 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,
- The variable 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#:
We can also use String.Equal() to compare a string with another string which might be null , it doesn’t throw any null reference error during 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 .
Conclusion:
In summary, you should choose the String.Equals() method in C#, when you need a simple and efficient way to compare strings 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#