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 exam
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.
This is a duplicate of
C# difference between == and Equals()
For some additional thoughts on different kinds of equality and how none of them do what you really want, see
http://blogs.msdn.com/ericlippert/archive/2009/04/09/double-your-dispatch-double-your-fun.aspx
This is more prominent in the Java world really. Basically '==' is operator overloading and .Equals() is the base method on Object class.
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.
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.
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.