"=="的定义Double运算子

编程入门 行业动态 更新时间:2024-10-21 18:42:02
本文介绍了"=="的定义Double运算子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

由于某种原因,我潜入了类的.NET Framework源代码. Double ,发现==的声明是:

For some reason I was sneaking into the .NET Framework source for the class Double and found out that the declaration of == is:

public static bool operator ==(Double left, Double right) { return left == right; }

每个运算符都适用相同的逻辑.

The same logic applies for every operator.

  • 这样的定义有什么意义?
  • 它如何工作?
  • 为什么不创建无限递归?
推荐答案

实际上,编译器会将==运算符转换为ceq IL代码,并且您提到的运算符将不会被调用.

In reality, the compiler will turn the == operator into a ceq IL code, and the operator you mention will not be called.

很可能在源代码中出现运算符的原因,因此可以从C#以外的其他语言调用该运算符,这些语言不会将其直接转换为CEQ调用(或通过反射). 运算符中的内代码将被编译为CEQ,因此没有无限递归.

The reason for the operator in the source code is likely so it can be called from languages other than C# that do not translate it into a CEQ call directly (or through reflection). The code within the operator will be compiled to a CEQ, so there is no infinite recursion.

实际上,如果通过反射调用运算符,则可以看到该运算符被调用(而不是CEQ指令),并且显然不是无限递归的(因为程序按预期终止):

In fact, if you call the operator via reflection, you can see that the operator is called (rather than a CEQ instruction), and obviously is not infinitely recursive (since the program terminates as expected):

double d1 = 1.1; double d2 = 2.2; MethodInfo mi = typeof(Double).GetMethod("op_Equality", BindingFlags.Static | BindingFlags.Public ); bool b = (bool)(mi.Invoke(null, new object[] {d1,d2}));

产生的IL(由LinqPad 4编译):

Resulting IL (compiled by LinqPad 4):

IL_0000: nop IL_0001: ldc.r8 9A 99 99 99 99 99 F1 3F IL_000A: stloc.0 // d1 IL_000B: ldc.r8 9A 99 99 99 99 99 01 40 IL_0014: stloc.1 // d2 IL_0015: ldtoken System.Double IL_001A: call System.Type.GetTypeFromHandle IL_001F: ldstr "op_Equality" IL_0024: ldc.i4.s 18 IL_0026: call System.Type.GetMethod IL_002B: stloc.2 // mi IL_002C: ldloc.2 // mi IL_002D: ldnull IL_002E: ldc.i4.2 IL_002F: newarr System.Object IL_0034: stloc.s 04 // CS$0$0000 IL_0036: ldloc.s 04 // CS$0$0000 IL_0038: ldc.i4.0 IL_0039: ldloc.0 // d1 IL_003A: box System.Double IL_003F: stelem.ref IL_0040: ldloc.s 04 // CS$0$0000 IL_0042: ldc.i4.1 IL_0043: ldloc.1 // d2 IL_0044: box System.Double IL_0049: stelem.ref IL_004A: ldloc.s 04 // CS$0$0000 IL_004C: callvirt System.Reflection.MethodBase.Invoke IL_0051: unbox.any System.Boolean IL_0056: stloc.3 // b IL_0057: ret

有趣的是-对于整数类型,只有Single,Double,Decimal,String和DateTime不存在相同的运算符(无论是在参考源中还是通过反射).从其他语言中调用它们的理论.显然,不用这些运算符,您就可以用其他语言将两个整数相等,因此我们回到了问题为什么double会存在它们"?

Interestingly - the same operators do NOT exist (either in the reference source or via reflection) for integral types, only Single, Double, Decimal, String, and DateTime, which disproves my theory that they exist to be called from other languages. Obviously you can equate two integers in other languages without these operators, so we're back to the question "why do they exist for double"?

更多推荐

"=="的定义Double运算子

本文发布于:2023-10-14 22:20:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492378.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义   quot   Double

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!