Does pointf resist versus point?

I need to compare PointF with a point as follows:

PointF myPointF = new PointF(1.1,1.1); Point myPoint = new Point(1,1); bool Equal = (myPointF == myPoint); 

But I want to know which of the following values โ€‹โ€‹is true or something else relates to how the comparison actually happens:

  • myPoint is converted to PointF first
  • myPointF is rounded to the point first
  • something completely different and unpredictable

You can write a test, but I want the answer guaranteed in the documentation.

In addition, there is an implicit conversion from Point to PointF , and not vice versa, so I think itโ€™s enough to say that the point is converted to PointF, but again, I donโ€™t know for sure.

+4
source share
2 answers

Using LinqPad, it's easy to take a look at IL to see what happens. Basically, Point converted to PointF , and then the equality == method is called for PointF .

For example, using this code:

 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); 

Produces the following 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 

If you use ILSpy, you can find the contents of both Point.op_Implicit and PointF.op_Equality .

 // System.Drawing.Point public static implicit operator PointF(Point p) { return new PointF((float)pX, (float)pY); } // System.Drawing.PointF public static bool operator ==(PointF left, PointF right) { return left.X == right.X && left.Y == right.Y; } 

This demonstrates that int values โ€‹โ€‹are converted to float values โ€‹โ€‹when Point converted to PointF , and then the float values โ€‹โ€‹from the original instance of PointF and the converted PointF > are compared.

+4
source

See what ildasm.exe shows:

 IL_001f: call valuetype [System.Drawing]System.Drawing.PointF [System.Drawing]System.Drawing.Point::op_Implicit(valuetype [System.Drawing]System.Drawing.Point) IL_0024: call bool [System.Drawing]System.Drawing.PointF::op_Equality(valuetype [System.Drawing]System.Drawing.PointF,valuetype [System.Drawing]System.Drawing.PointF) 

So yes. A point is considered as PointF for comparison.

+3
source

All Articles