The sizeof operator can be applied only to value types, not reference types.
Note |
|---|
| From version 2.0 of C# onwards, applying sizeof to predefined types no longer requires that unsafe mode be used. |
The sizeof operator may not be overloaded. The values returned by the sizeof operator are of the type int. The following table shows the constant values that represent the sizes of certain predefined types.
| Expression | Result |
| sizeof(sbyte) | 1 |
| sizeof(byte) | 1 |
| sizeof(short) | 2 |
| sizeof(ushort) | 2 |
| sizeof(int) | 4 |
| sizeof(uint) | 4 |
| sizeof(long) | 8 |
| sizeof(ulong) | 8 |
| sizeof(char) | 2 (Unicode) |
| sizeof(float) | 4 |
| sizeof(double) | 8 |
| sizeof(bool) | 1 |
For all other types, including structs, the sizeof operator can only be used in unsafe code blocks. Although you can use the SizeOf method, the value returned by this method is not always the same as the value returned by sizeof. Marshal.SizeOf returns the size after the type has been marshaled whereas sizeof returns the size as it has been allocated by the Common Language Runtime, including any padding.