I used to think that strA.Equals(strB) is faster than strA == strB, so I always used it. But that's not entirely accurate.
== is the identity test. It checks wheather two objects are one and the same. This is the faster of the two.
.Equals() asks one object to compare it's value against another and decide whether or not they are equal.
Since value types are kept on the stack, and not on the heap, they can only be compared by value. The ValueType-base class uses reflection to compare each property. Value types, like structs, should therefore override .Equals() and == to speed things up.
One last consideration is that strA.Equals(strB) requires strA to be an instantiated object, whereas strA == strB allows strA to be NULL.
Conclusion: comparing two strings using == is the same as doing it with .Equals().
No comments:
Post a Comment