G
_gh_manvel_
Guest
I have a readonly struct
public readonly struct ReadonlyPoint3D
{
public ReadonlyPoint3D(double x, double y, double z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public double X { get; }
public double Y { get; }
public double Z { get; }
}
I have a method that accepts readonly struct as a parameter passed by in keyword
private static double CalculateDistance(in ReadonlyPoint3D point1, in ReadonlyPoint3D point2)
{
double xDifference = point1.X - point2.X;
double yDifference = point1.Y - point2.Y;
double zDifference = point1.Z - point2.Z;
return Math.Sqrt(xDifference * xDifference + yDifference * yDifference + zDifference * zDifference);
}
If I look at generated IL code, it seems that readonly struct passed to CalculateDistance method is being copied in the method.
// [43 9 - 43 10]
IL_0000: nop
// [44 13 - 44 54]
IL_0001: ldarg.0 // point1
IL_0002: call instance float64 CSharpTests.ReadonlyPoint3D::get_X()
IL_0007: ldarg.1 // point2
IL_0008: call instance float64 CSharpTests.ReadonlyPoint3D::get_X()
IL_000d: sub
IL_000e: stloc.0 // xDifference
// the rest is omitted for the sake of brevity
My understanding was that readonly structs passed to a method with in modifier shouldn't be copied.
Did I understand it incorrectly, or I'm interpreting the generated IL code in a wrong way ?
Continue reading...
public readonly struct ReadonlyPoint3D
{
public ReadonlyPoint3D(double x, double y, double z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public double X { get; }
public double Y { get; }
public double Z { get; }
}
I have a method that accepts readonly struct as a parameter passed by in keyword
private static double CalculateDistance(in ReadonlyPoint3D point1, in ReadonlyPoint3D point2)
{
double xDifference = point1.X - point2.X;
double yDifference = point1.Y - point2.Y;
double zDifference = point1.Z - point2.Z;
return Math.Sqrt(xDifference * xDifference + yDifference * yDifference + zDifference * zDifference);
}
If I look at generated IL code, it seems that readonly struct passed to CalculateDistance method is being copied in the method.
// [43 9 - 43 10]
IL_0000: nop
// [44 13 - 44 54]
IL_0001: ldarg.0 // point1
IL_0002: call instance float64 CSharpTests.ReadonlyPoint3D::get_X()
IL_0007: ldarg.1 // point2
IL_0008: call instance float64 CSharpTests.ReadonlyPoint3D::get_X()
IL_000d: sub
IL_000e: stloc.0 // xDifference
// the rest is omitted for the sake of brevity
My understanding was that readonly structs passed to a method with in modifier shouldn't be copied.
Did I understand it incorrectly, or I'm interpreting the generated IL code in a wrong way ?
Continue reading...