C#Equals()==关于数字 [英] C# Equals() == about number
问题描述
我尝试以下代码在数字比较中使用 ==
和 Equals
:
I try following code use ==
and Equals
on number comparison:
Console.WriteLine( (int)2 == (double)2.0 );
Console.WriteLine( ( (int)2 ).Equals( (double)2.0) );
Console.WriteLine((float)2.0 == (double)2.0);
Console.WriteLine( ( (float)2.0 ).Equals( (double)2.0 ) );
结果:
true
false
true
false
阅读帖子后,
int,double,float
均为 ValueType
Here2 ,我仍然不明白为什么 ==
和 Equals
得出不同的结果,
int, double, float
are all ValueType
, after reading posts Here1 and Here2, I still cannot understand why ==
and Equals
turns out different result,
在这4种关于数字的情况下, ==
和 Equals
背后的工作细节是什么?
What is the working detail behind ==
and Equals
in these 4 cases about number?
(如果该问题重复,请告诉我)
(if this question is duplicate please tell me)
编辑:另外4种有趣的情况:
4 more interesting cases:
双精度浮点数<-> int
double, float <-> int
Console.WriteLine((double)2.0 == (int)2); //True
Console.WriteLine(((double)2.0).Equals((int)2)); //True
Console.WriteLine((float)2.0 == (int)2.0); //True
Console.WriteLine(((float)2.0).Equals((int)2.0)); //True
double,int<->浮点数
double, int <-> float
Console.WriteLine((double)2.0 == (float)2.0); //True
Console.WriteLine(((double)2.0).Equals((float)2.0)); //True
Console.WriteLine((int)2 == (float)2.0); //True
Console.WriteLine(((int)2).Equals((float)2.0)); //False
推荐答案
(int)2 == (double)2.0 - True because the compiler promotes int to double when comparing via ==.
((int)2).Equals( (double)2.0) - False because this is calling int.Equals(object) and the types are different.
(float)2.0 == (double)2.0 - True because the compiler promotes float to double when comparing via ==.
((float)2.0).Equals((double)2.0) - False becaue this is calling float.Equals(object) and the types are different.
(double)2.0 == (int)2 - True because the compiler promotes int to double when comparing via ==.
((double)2.0).Equals((int)2) - True because there exists double.Equals(double) and the compiler
promotes the integer parameter 2 to double to call double.Equals(2.0).
(float)2.0 == (int)2.0 - True because the compiler promotes int to float when comparing via ==.
((float)2.0).Equals((int)2.0) - True because there exists float.Equals(float) and the compiler
promotes the integer parameter 2 to float to call float.Equals(2.0f).
(double)2.0 == (float)2.0) - True because the compiler promotes float to double when comparing via ==.
((double)2.0).Equals((float)2.0) - True because there exists double.Equals(double) and the compiler
promotes the float parameter 2.0f to double to call double.Equals(2.0).
(int)2 == (float)2.0 - True because the compiler promotes int to float when comparing via ==.
((int)2).Equals((float)2.0) - False because this is calling int.Equals(object) and the types are different.
请注意,在返回 false
的情况下,这是因为尽管存在 int.Equals(int)
,但编译器无法调用它,因为没有自动类型转换从浮点类型到 int
.
Note that in the cases where false
is returned, it's because although int.Equals(int)
exists, the compiler cannot call it because there is no automatic type conversion from floating point types to int
.
这篇关于C#Equals()==关于数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!