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():

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:

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:

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:

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.

Here, String.Equals() gracefully handles the comparison and returns false .


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#