The sizeof operator is used to obtain the size in bytes for a value type. A sizeof expression takes the form:
sizeof(type)
where:
The sizeof operator can be applied only to value types, not reference types.
The sizeof operator can only be used in the unsafe mode.
The sizeof operator cannot be overloaded.
// cs_operator_sizeof.cs
// compile with: /unsafe
// Using the sizeof operator
using System;
class SizeClass
{
// Notice the unsafe declaration of the method:
unsafe public static void SizesOf()
{
Console.WriteLine("The size of short is {0}.", sizeof(short));
Console.WriteLine("The size of int is {0}.", sizeof(int));
Console.WriteLine("The size of long is {0}.", sizeof(long));
}
}
class MainClass
{
public static void Main()
{
SizeClass.SizesOf();
}
}
The size of short is 2. The size of int is 4. The size of long is 8.