I am trying to check size of all of my variables (value types) using sizeof operator. I gone through one of the msdn article where it is written that
For all other types, including structs, the sizeof operator can be used only in unsafe code blocks
and also structs should not contain any fields or properties that are reference types
For this, I enabled unsafe compilation in my project properties and created structure as follows-
struct EmployeeStruct
{
int empId;
long salary;
}
and used it as follows-
unsafe
{
size = sizeof(EmployeeStruct);
}
Console.WriteLine("Size of type in bytes is: {0}", size);
Here I am getting output as Size of type in bytes is: 16 however by looking at structure it should be 12 (4 for int and 8 for long). Can someone help me understand here that why I am getting 4 byte extra size?