Why would you use String.Equals over ==? [duplicate]
It's entirely likely that a large portion of the developer base comes from a Java background where using ==
to compare strings is wrong and doesn't work.
In C# there's no (practical) difference (for strings) as long as they are typed as string.
If they are typed as object
or T
then see other answers here that talk about generic methods or operator overloading as there you definitely want to use the Equals method.
There is practical difference between string.Equals
and ==
bool result = false;
object obj = "String";
string str2 = "String";
string str3 = typeof(string).Name;
string str4 = "String";
object obj2 = str3;
// Comparision between object obj and string str2 -- Com 1
result = string.Equals(obj, str2);// true
result = String.ReferenceEquals(obj, str2); // true
result = (obj == str2);// true
// Comparision between object obj and string str3 -- Com 2
result = string.Equals(obj, str3);// true
result = String.ReferenceEquals(obj, str3); // false
result = (obj == str3);// false
// Comparision between object obj and string str4 -- Com 3
result = string.Equals(obj, str4);// true
result = String.ReferenceEquals(obj, str4); // true
result = (obj == str4);// true
// Comparision between string str2 and string str3 -- Com 4
result = string.Equals(str2, str3);// true
result = String.ReferenceEquals(str2, str3); // false
result = (str2 == str3);// true
// Comparision between string str2 and string str4 -- Com 5
result = string.Equals(str2, str4);// true
result = String.ReferenceEquals(str2, str4); // true
result = (str2 == str4);// true
// Comparision between string str3 and string str4 -- Com 6
result = string.Equals(str3, str4);// true
result = String.ReferenceEquals(str3, str4); // false
result = (str3 == str4);// true
// Comparision between object obj and object obj2 -- Com 7
result = String.Equals(obj, obj2);// true
result = String.ReferenceEquals(obj, obj2); // false
result = (obj == obj2);// false
Adding Watch
obj "String" {1#} object {string}
str2 "String" {1#} string
str3 "String" {5#} string
str4 "String" {1#} string
obj2 "String" {5#} object {string}
Now look at {1#}
and {5#}
obj
, str2
, str4
and obj2
references are same.
obj
and obj2
are object type
and others are string type
Conclusion:
-
com1: result = (obj == str2);// true
- compares
object
andstring
so performs a reference equality check - obj and str2 point to the same reference so the result is true
- compares
-
com2: result = (obj == str3);// false
- compares
object
andstring
so performs a reference equality check - obj and str3 point to the different references so the result is false
- compares
-
com3: result = (obj == str4);// true
- compares
object
andstring
so performs a reference equality check - obj and str4 point to the same reference so the result is true
- compares
-
com4: result = (str2 == str3);// true
- compares
string
andstring
so performs a string value check - str2 and str3 are both "String" so the result is true
- compares
-
com5: result = (str2 == str4);// true
- compares
string
andstring
so performs a string value check - str2 and str4 are both "String" so the result is true
- compares
-
com6: result = (str3 == str4);// true
- compares
string
andstring
so performs a string value check - str3 and str4 are both "String" so the result is true
- compares
-
com7: result = (obj == obj2);// false
- compares
object
andobject
so performs a reference equality check - obj and obj2 point to the different references so the result is false