Why sizeof(Point) is 8?

443 views Asked by At

i wrote code and get strange result- integer i is 8:

unsafe
        {
            int i = sizeof(Point);
        }

After checking struct Point i found this fields:

    public bool IsEmpty { get; }
    public int X { get; set; }
    public int Y { get; set; }

bits math: 32+32+1 = 65 bits, so is > 8 bytes

So, why sizeof returns 8 , but not 9?

Thanks

2

There are 2 answers

5
Joey On BEST ANSWER

IsEmpty is a property, not a field. Properties are just methods behind the scenes, so they're not part of the size of a structure.

0
JimiLoe On

The framework implementation of Point uses only two attributes:

private int x;
private int y; 

Empty is implemented as

[Browsable(false)]
public bool IsEmpty { 
    get {
        return x == 0 && y == 0;
    }
} 

The two int fields occupy 8 bytes - and everything is fine.