17

This is a purely academic question, but what's the difference between using == and .Equals within a lambda expression and which one is preferred?

Code examples:

int categoryId = -1;
listOfCategories.FindAll(o => o.CategoryId == categoryId);

or

int categoryId = -1; 
listOfCategories.FindAll(o => o.CategoryId.Equals(categoryId));
Robert W
  • 2,921
  • 7
  • 34
  • 42
  • 13
    Why should "== vs. Equals in Lambda expressions" be different than "== vs. Equals in general"? – Heinzi Feb 16 '10 at 14:37
  • 7
    @Heinzi, for lambdas which are expression trees (that is, not the syntax `x => {}` which is a shortcut for an anonymous delegate), there may be a difference depending on who (which provider) evaluates the expression tree. – Lucero Feb 16 '10 at 14:43
  • @FrustratedWithFormsDesigner - CategoryId is an int, and I'll update the question with this info as well. – Robert W Feb 16 '10 at 15:15

5 Answers5

9

For reference types, == is intended to communicate reference equality — do the two variables refer to the same object instance.
.Equals() is intended to communicate value equality — do the two probably different object instances referred to by the two variables have the same value, for some definition of "same" that you provide by overloading the method.

For value types, those two meanings are blurred.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 3
    Actually, that's more the distinction is Java, where operator== cannot be overloaded to show value equality. – James Curran Feb 16 '10 at 14:44
  • 4
    Hm, what about the `string` type? It's a reference type, yet `==` looks for value equality, not reference equality. – Lucero Feb 16 '10 at 14:45
  • 3
    System.String is a RTINO (reference type in name only). It is implemented as a reference type for optimization reasons. It is designed to be treated in every way by the consumer like a value type. – Michael Meadows Feb 16 '10 at 14:49
  • @Lucero: Any reference type may overload `==`, but absent an overload, Joel's answer is correct; the default behaviour of `==` for a reference type is reference-equality. – Aaronaught Feb 16 '10 at 14:53
  • @Michael... um.. That would make it a RTIIO (reference type in implementation only) – James Curran Feb 16 '10 at 17:27
2

They can be overloaded separately, so they could provide different answers. See http://msdn.microsoft.com/en-us/library/ms173147(VS.80).aspx which discusses how to overload each. Typically they will be the same, but there's no guarantee of that. So it depends on what type the lambda object is.

Scott Stafford
  • 43,764
  • 28
  • 129
  • 177
1

The Lambda is irrelevant here...

For value objects == and equals are the same For reference object == will be true if the objects are the same object (points to the same instance) while it is expected that equals compare the contents of the objects. This link explains it in another way.

Arve
  • 7,284
  • 5
  • 37
  • 41
0

It depends on what defined for the object. If there's no operator== defined for the class, it will use the one from the Object class, which checks Object.ReferenceEquals before eventually calling Equals().

This shows an important distinction: if you say A.Equals(B) then A must be nun-null. if you say A == B, A may be null.

James Curran
  • 101,701
  • 37
  • 181
  • 258
0

This is more prominent in the Java world really. Basically '==' is operator overloading and .Equals() is the base method on Object class.

mtmk
  • 6,176
  • 27
  • 32