Problem
I was just exposed to a large codebase and found that String is used for all string comparisons. Instead of ==, use equals().
Do you suppose there’s a purpose for this?
Asked by JamesBrownIsDead
Solution #1
It’s very likely that a significant section of the developer community has come from a Java background, where utilizing == to compare strings is incorrect and ineffective.
In C#, as long as strings are typed as strings, there is no (practical) difference.
If they’re object or T, look for other answers here that discuss generic methods or operator overloading, as you’ll want to use the Equals method there.
Answered by Matthew Scharley
Solution #2
There is a practical distinction between string and wire. == and equals
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}
Take a look at 1# and 5# now.
The references to obj, str2, str4, and obj2 are all the same.
The object types obj and obj2 are object types, while the string types are string types.
Conclusion:
Answered by vikas
Solution #3
There is one minor but critical distinction between == and the String. Methods that are equal:
class Program
{
static void Main(string[] args)
{
CheckEquality("a", "a");
Console.WriteLine("----------");
CheckEquality("a", "ba".Substring(1));
}
static void CheckEquality<T>(T value1, T value2) where T : class
{
Console.WriteLine("value1: {0}", value1);
Console.WriteLine("value2: {0}", value2);
Console.WriteLine("value1 == value2: {0}", value1 == value2);
Console.WriteLine("value1.Equals(value2): {0}", value1.Equals(value2));
if (typeof(T).IsEquivalentTo(typeof(string)))
{
string string1 = (string)(object)value1;
string string2 = (string)(object)value2;
Console.WriteLine("string1 == string2: {0}", string1 == string2);
}
}
}
Produces this output:
As you can see, the == operator returns false for two strings that are clearly equal. Why? Because the generic method’s == operator is resolved to the op equal method as described by System. Object (the method’s only guarantee of T at build time), which signifies reference equality rather than value equality.
Because the compiler resolves the == to System when two values are expressly typed as System.String, == has a value-equality interpretation. Instead of System, use String.op equal. Object.op equal.
As a result, I usually always use String to be safe. Instead, I always receive the value equality semantics I want when I use equals.
I usually use the static String to avoid NullReferenceExceptions if one of the values is null. method equals:
bool true = String.Equals("a", "ba".Substring(1));
Answered by Andrew Arnott
Solution #4
String. Equals has overloads to deal with casing and culture-aware comparison. If your code doesn’t use these, the developers may be used to Java, where content comparisons must be done with the.Equals function, as Matthew points out.
Answered by Michael Petrotta
Solution #5
In terms of functionality, both methods compare values. As stated on the Microsoft Developer Network (MSDN):
These techniques behave differently if one of your string instances is null:
string x = null;
string y = "qq";
if (x == y) // returns false
MessageBox.Show("true");
else
MessageBox.Show("false");
if (x.Equals(y)) // returns System.NullReferenceException: Object reference not set to an instance of an object. - because x is null !!!
MessageBox.Show("true");
else
MessageBox.Show("false");
Answered by thezar
Post is based on https://stackoverflow.com/questions/1659097/why-would-you-use-string-equals-over