The size of the byte used to obtain the value type. For example, the int size can be retrieved as follows:
Int intSize = sizeof (int );
Console. WriteLine (intSize );
The sizeof operator applies only to the value type, not the reference type.
The sizeof operator cannot be overloaded. The value returned by the sizeof operator is of the int type. The following table shows the constant values of some 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 struct), the sizeof operator can only be used in insecure code blocks. Although the SizeOf method can be used, the values returned by this method are not always the same as those returned by sizeof. Inclual. SizeOf returns the size after the encapsulated processing type, while sizeof returns the size allocated by the Common Language Runtime (including any blank space ).
This article is a new one. For more information, see the source and author!