Thursday, November 24, 2005

== does not equal Equals()

I discovered that for boxed System.ValueType the == operator behaves differently from the Equals() function. When using == with two boxed ValueType-s it will compare their references, thus "always" returning false. Equals(), on the other hand, has been overloaded by Microsoft for the ValuType-s, so that it does a value comparison of the boxed objects, giving the expected result.

I wonder why Microsoft did not also overload the == operator?

2 comments:

Anonymous said...

Ravi Gyani does an excellent job of explaining equality in C# here

Ari said...

Interesting article. Thanks for the link!