if (<object> == )

后端 未结 3 477
故里飘歌
故里飘歌 2021-01-19 23:12

I can\'t understand, why the if statement in the picture below returns false. I hope you can explain it to me.

You can see, that the values and the typs

3条回答
  •  太阳男子
    2021-01-19 23:48

    The problem is that the two object wrappers of int that you are comparing are different objects.

    Since object does not override == with a call of Equals, the comparison checks references instead, and returns false, because the two objects are different.

    You can use object.Equals(item, value) to avoid this problem.

提交回复
热议问题