Lambda Expression: == vs. .Equals()

前端 未结 6 785
名媛妹妹
名媛妹妹 2021-01-07 23:09

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

6条回答
  •  心在旅途
    2021-01-07 23:34

    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.

提交回复
热议问题