我尝试以下代码使用==和Equals数字比较:
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 ) );
Run Code Online (Sandbox Code Playgroud)
结果:
true
false
true
false
Run Code Online (Sandbox Code Playgroud)
int, double, float都是ValueType,在阅读了帖子Here1和Here2后,我仍然无法理解为什么==结果Equals不同,
这4个案例==背后的工作细节是什么?Equals
(如果这个问题重复,请告诉我)
编辑: 4个更有趣的案例:
双精度型、浮点型 <-> 整数型
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
Run Code Online (Sandbox Code Playgroud)
双精度型、整型 <-> 浮点型
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
Run Code Online (Sandbox Code Playgroud)
来自MSDN:
ValueType.Equals 指示此实例和指定对象是否相等。
和
返回值:
类型:系统.布尔值
true 如果 obj 和此实例是相同类型并且表示相同值;否则,错误。*
如果你这样做:
int a = 1;
double b = a;
bool check = a.Equals(b);
Run Code Online (Sandbox Code Playgroud)
您正在调用 Equals 的此实现:
[__DynamicallyInvokable]
public override bool Equals(object obj)
{
if (!(obj is int))
return false;
return this == (int) obj;
}
Run Code Online (Sandbox Code Playgroud)
如果你这样做:
int a = 1;
int b = a;
bool check = a.Equals(b);
Run Code Online (Sandbox Code Playgroud)
您正在称其为:
[NonVersionable]
[__DynamicallyInvokable]
public bool Equals(int obj)
{
return this == obj;
}
Run Code Online (Sandbox Code Playgroud)
(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.
Run Code Online (Sandbox Code Playgroud)
false请注意,在返回 的情况下,这是因为虽然int.Equals(int)存在,但编译器无法调用它,因为没有从浮点类型到 的自动类型转换int。
| 归档时间: |
|
| 查看次数: |
6192 次 |
| 最近记录: |