Game Development Reference
In-Depth Information
Getting Size of Data Types
Just as in C\C++, you can use the sizeof operator to determine the number of
bytes occupied of the given data type. You must do so within an unsafe context.
The following code can be executed to print out a list of data type sizes for handy
reference. As of .NET 2.0, it is optional to use the sizeof operator within an unsafe
Console.WriteLine(“sbyte: {0}”, sizeof(sbyte));
Console.WriteLine(“byte: {0}”, sizeof(byte));
Console.WriteLine(“short: {0}”, sizeof(short));
Console.WriteLine(“ushort: {0}”, sizeof(ushort));
Console.WriteLine(“int: {0}”, sizeof(int));
Console.WriteLine(“uint: {0}”, sizeof(uint));
Console.WriteLine(“long: {0}”, sizeof(long));
Console.WriteLine(“ulong: {0}”, sizeof(ulong));
Console.WriteLine(“char: {0}”, sizeof(char));
Console.WriteLine(“float: {0}”, sizeof(float));
Console.WriteLine(“double: {0}”, sizeof(double));
Console.WriteLine(“decimal: {0}”, sizeof(decimal));
Console.WriteLine(“bool: {0}”, sizeof(bool));
Executing the above code will print out the following:
sbyte: 1
byte: 1
short: 2
ushort: 2
int: 4
uint: 4
long: 8
ulong: 8
char: 2
float: 4
double: 8
decimal: 16
bool: 1
You may be wondering why char prints a size of 2 . This is because System.Char
(char) is a Unicode type (two bytes), and sizeof returns the size of the data types
allocated by the CLR. There is another method to get the size of data types after
Search Nedrilad ::

Custom Search