Lambda Expression: == vs. .Equals()

前端 未结 6 821
名媛妹妹
名媛妹妹 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:28

    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.

提交回复
热议问题