Here you will learn which is the best way to check whether the two strings are equal or not in C#.
You can check the equality of strings using two ways:
- Using == operator
- Using Equals() method
Let's see different scenarios of comparing string equalities.
Compare Case-Sensitive Strings
Both, == and
Equals() method compares the content of strings. So, there is no difference between them when you compare strings case-sensitive and in en culture.
string str1 = "London"; string str2 = "London"; str1 == str2; // true str1.Equals(str2); // true
What happens if a string is null?
string str1 = "London"; string str2 = null; str1 == str2; // false str1.Equals(str2); // false str2.Equals(str1); // NullReferenceException
As you can see above, there is no problem with == operator if a string is null. But, calling the
Equals() method on null will throw the
NullReferenceException. So, you have to make sure a string is not null before calling the
Now, consider the following example where comparing a string with an object type.
string str1 = "London"; object obj = "London"; str1 == obj; // true str1.Equals( obj); // true obj.Equals(str1); // true
So, it gives the correct result when comparing a string with an object.
Now, let's see a little complicated scenario.
string str = "London"; StringBuilder sb = new StringBuilder("London"); object obj = sb.ToString(); str == obj; // false str == sb.ToString();// true str.Equals(obj);// true obj.Equals(str1); //true
In the above example,
str == obj returns false even though the values are the same. Why?
The String type implements == operator overload, which compares the value of two operands. However, after casting StringBuilder to object, it calls different overloads where it compares reference of the two operands. So,
str == obj gives the wrong result.
So, if you are comparing strings case-sensitive, then in most cases == and
Equals() will behave the same. However, in the scenario like above, == gives the wrong result.
Compare Case-Insensitive Strings
The == operator always compares strings case-sensitive.
string str1 = "LONDON"; string str2 = "london"; str1 == str2; //false
Equals() method to compare strings case-insensitive using StringComparison parameter.
string str1 = "LONDON"; string str2 = "london"; str1.Equals(str2, StringComparison.CurrentCultureIgnoreCase); // true
Always make sure that string is not null using null-conditional operator
? before calling
Equals() method, as shown below.
string str1 = null; string str2 = "london"; str1?.Equals(str2, StringComparison.CurrentCultureIgnoreCase); // true
== vs Equals
|Compares the content of strings.||Compares the content of strings.|
|Always compares case-sensitive.||Compares case-sensitive or insensitive, culture specific or invariant culture strings using StringComparison enum.|
|Compares null values also.||Throws NullReferenceException if string is null.|
|Cannot be overloaded.||Can be overloaded to customize it.|
If you are sure that the type of two operands are string and want to compare string case-sensitive, then both will give the right result. However, you don't know the type of operands and want to compare strings case-insensitive or want to compare culture-specific strings then use the
Equals() method. Just make sure that a string is not null on which you call the
Learn about string comparison in detail.
- How to validate email in C#?
- How to get the sizeof a datatype in C#?
- Difference between String and StringBuilder in C#
- Static vs Singleton in C#
- Difference between == and Equals() Method in C#
- Asynchronous programming with async, await, Task in C#
- How to loop through an enum in C#?
- Generate Random Numbers in C#
- Difference between Two Dates in C#
- Convert int to enum in C#
- BigInteger Data Type in C#
- Convert String to Enum in C#
- Convert an Object to JSON in C#
- Convert JSON String to Object in C#
- DateTime Formats in C#
- How to convert date object to string in C#?
- How to count elements in C# array?
- Difference between String and string in C#.
- How to get a comma separated string from an array in C#?
- Boxing and Unboxing in C#
- How to convert string to int in C#?