gpt4 book ai didi

c# - 为什么 0L 在转换为对象时不等于 0?

转载 作者:太空狗 更新时间:2023-10-29 22:16:01 25 4
gpt4 key购买 nike

我想我不明白为什么第一个语句的计算结果为 true 而最后一个语句的计算结果为 false,但这是漫长的一天。

谁能解释一下?

0L.Equals(0) // true
((object)0L).Equals(0L) // true
((object)0L).Equals(0) // false

最佳答案

Object.Equals 首先比较对象是值类型的类型。在这种情况下两者是不同的。

MSDN :

If the current instance is a value type, the Equals(Object) method tests for value equality. Value equality means the following: The two objects are of the same type. As the following example shows, a Byte object that has a value of 12 does not equal an Int32 object that has a value of 12, because the two objects have different run-time types.

关于c# - 为什么 0L 在转换为对象时不等于 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27850042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com