Why is sizeof (Point) equal to 8?
I wrote some code and I get a weird integer value i: 8:
unsafe
{
int i = sizeof(Point);
}
After checking the struct Point, I found these fields:
public bool IsEmpty { get; }
public int X { get; set; }
public int Y { get; set; }
bit math: 32 + 32 + 1 = 65 bits, so> 8 bytes
So why does sizeof return 8 but not 9?
thank
+3
zzfima
source
to share
2 answers
IsEmpty
is a property, not a field. Properties are just methods behind the scenes, so they are not part of the structure's size.
+10
Joey
source
to share
the framework implementation Point
only uses two attributes:
private int x;
private int y;
Empty
implemented as
[Browsable(false)]
public bool IsEmpty {
get {
return x == 0 && y == 0;
}
}
Two fields int
take 8 bytes - and you're fine.
+5
JimiLoe
source
to share