使用 LinqPad,很容易查看 IL 以了解发生了什么。基本上,Point
转换为PointF
,然后==
调用相等方法PointF
。
例如,使用以下代码:
System.Drawing.PointF myPointF = new System.Drawing.PointF(1.1f ,1.1f);
System.Drawing.Point myPoint = new System.Drawing.Point(1,1);
bool equal = (myPointF == myPoint);
Console.WriteLine(equal);
产生以下 IL:
IL_0001: ldloca.s 00 // myPointF
IL_0003: ldc.r4 CD CC 8C 3F
IL_0008: ldc.r4 CD CC 8C 3F
IL_000D: call System.Drawing.PointF..ctor
IL_0012: nop
IL_0013: ldloca.s 01 // myPoint
IL_0015: ldc.i4.1
IL_0016: ldc.i4.1
IL_0017: call System.Drawing.Point..ctor
IL_001C: nop
IL_001D: ldloc.0 // myPointF
IL_001E: ldloc.1 // myPoint
IL_001F: call System.Drawing.Point.op_Implicit //<- convert Point to PointF
IL_0024: call System.Drawing.PointF.op_Equality //<- PointF equality
IL_0029: stloc.2 // equal
IL_002A: ldloc.2 // equal
IL_002B: call System.Console.WriteLine
如果你使用 ILSpy,你可以找到 和 的Point.op_Implicit
内容PointF.op_Equality
。
// System.Drawing.Point
public static implicit operator PointF(Point p)
{
return new PointF((float)p.X, (float)p.Y);
}
// System.Drawing.PointF
public static bool operator ==(PointF left, PointF right)
{
return left.X == right.X && left.Y == right.Y;
}
这表明当转换为 a时,这些int
值被转换为float
值,然后将原始实例和转换后的实例的值进行比较。Point
PointF
float
PointF
PointF