c# - 为什么 long 和 decimal 之间的 Equals 不可交换?

标签 c# .net

我在 linqpad 中运行了这段代码:

    long x = long.MaxValue;
    decimal y = x;

    x.Dump();
    y.Dump();

    (x == y).Dump();
    (y == x).Dump();

    Object.Equals(x, y).Dump();
    Object.Equals(y, x).Dump();
    x.Equals(y).Dump();
    y.Equals(x).Dump();

它产生这个输出:

    9223372036854775807
    9223372036854775807
    True
    True
    False
    False
    False
    True

请注意最后两行:x.Equals(y) 为假,但 y.Equals(x) 为真。因此 decimal 认为自己等于具有相同值的 long 但 long 不认为自己等于具有相同值的 decimal。

这种行为的解释是什么?

更新:

我接受了李的回答。

我对此很好奇,写了这个小程序:

using System;
namespace TestConversion
{
  class Program
  {
    static void Main(string[] args)
    {
      long x = long.MaxValue;
      decimal y = x;

      Console.WriteLine(x);
      Console.WriteLine(y);

      Console.WriteLine(x == y);
      Console.WriteLine(y == x);

      Console.WriteLine(Object.Equals(x, y));
      Console.WriteLine(Object.Equals(y, x));
      Console.WriteLine(x.Equals(y));
      Console.WriteLine(y.Equals(x));
      Console.ReadKey();
    }
  }
}

然后我在 IL 中对其进行了反汇编:

.method private hidebysig static void Main(string[] args) cil managed
{
  .entrypoint
  .maxstack 2
  .locals init (
    [0] int64 x,
    [1] valuetype [mscorlib]System.Decimal y)
  L_0000: nop 
  L_0001: ldc.i8 9223372036854775807
  L_000a: stloc.0 
  L_000b: ldloc.0 
  L_000c: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Implicit(int64)
  L_0011: stloc.1 
  L_0012: ldloc.0 
  L_0013: call void [mscorlib]System.Console::WriteLine(int64)
  L_0018: nop 
  L_0019: ldloc.1 
  L_001a: call void [mscorlib]System.Console::WriteLine(valuetype [mscorlib]System.Decimal)
  L_001f: nop 
  L_0020: ldloc.0 
  L_0021: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Implicit(int64)
  L_0026: ldloc.1 
  L_0027: call bool [mscorlib]System.Decimal::op_Equality(valuetype [mscorlib]System.Decimal, valuetype [mscorlib]System.Decimal)
  L_002c: call void [mscorlib]System.Console::WriteLine(bool)
  L_0031: nop 
  L_0032: ldloc.1 
  L_0033: ldloc.0 
  L_0034: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Implicit(int64)
  L_0039: call bool [mscorlib]System.Decimal::op_Equality(valuetype [mscorlib]System.Decimal, valuetype [mscorlib]System.Decimal)
  L_003e: call void [mscorlib]System.Console::WriteLine(bool)
  L_0043: nop 
  L_0044: ldloc.0 
  L_0045: box int64
  L_004a: ldloc.1 
  L_004b: box [mscorlib]System.Decimal
  L_0050: call bool [mscorlib]System.Object::Equals(object, object)
  L_0055: call void [mscorlib]System.Console::WriteLine(bool)
  L_005a: nop 
  L_005b: ldloc.1 
  L_005c: box [mscorlib]System.Decimal
  L_0061: ldloc.0 
  L_0062: box int64
  L_0067: call bool [mscorlib]System.Object::Equals(object, object)
  L_006c: call void [mscorlib]System.Console::WriteLine(bool)
  L_0071: nop 
  L_0072: ldloca.s x
  L_0074: ldloc.1 
  L_0075: box [mscorlib]System.Decimal
  L_007a: call instance bool [mscorlib]System.Int64::Equals(object)
  L_007f: call void [mscorlib]System.Console::WriteLine(bool)
  L_0084: nop 
  L_0085: ldloca.s y
  L_0087: ldloc.0 
  L_0088: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Implicit(int64)
  L_008d: call instance bool [mscorlib]System.Decimal::Equals(valuetype [mscorlib]System.Decimal)
  L_0092: call void [mscorlib]System.Console::WriteLine(bool)
  L_0097: nop 
  L_0098: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
  L_009d: pop 
  L_009e: ret 
}

确实可以看到long值转为decimal了。

谢谢大家!

最佳答案

这是因为在

y.Equals(x);

正在调用 decimal.Equals(decimal) 重载,因为有一个 implicit conversionlongdecimal 之间。结果,比较返回 true。

但是,由于没有从 decimallong 的隐式转换

x.Equals(y)

调用 long.Equals(object) 导致 y 被装箱并且比较返回 false 因为它不能被拆箱为 long .

关于c# - 为什么 long 和 decimal 之间的 Equals 不可交换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28118563/

相关文章:

c# - Eratosthenes 筛法改进后运行速度变慢

c# - 如何在 C# 中打印 KeyValuePair<K,T> 中的信息?

c# - .Net 相当于 JCEKS keystore

.net - CompositionBatch : AddPart vs AddExport

c# - NEST2:如何一次指定数据库索引名称

c# - 带有 nuget 的 MS Build Extension Pack 无法获取 MSBuild.ExtensionPack.tasks

php - PHP 的 fopen 和 fwrite 的 VB.NET 版本是什么(如果存在)?

c# - 使用Lock来停止并发执行不起作用?

c# - 为什么我们不能在 C# 中进行 IntPtr 和 UIntPtr 运算?

c# - 比较两个xml文件并显示差异