C Sharp

sizeof

The sizeof operator is used to obtain the size, in bytes, of a given type. You should keep in mind two extremely important factors when using this operator. First, you can use the sizeof operator with value types only. Therefore, although this operator can be used on class members, it cannot be used on the class type itself. Second, this operator can be used only in a method or code block marked as unsafe. We'll get into unsafe code in Chapter 17, "Interoperating with Unmanaged Code." Here's an example of using the sizeof operator from a class method marked as unsafe: -

using System;
class BasicTypes
{
    // NOTE: You must declare that code that uses the sizeof
    // operator as unsafe.
    static unsafe public void ShowSizes()
    {
        Console.WriteLine("\nBasic type sizes");
        Console.WriteLine("sizeof short = {0}", sizeof(short));
        Console.WriteLine("sizeof int = {0}", sizeof(int));
        Console.WriteLine("sizeof long = {0}", sizeof(long));
        Console.WriteLine("sizeof bool = {0}", sizeof(bool));
    }
}
class Unsafe1App
{
    unsafe public static void Main()
    {
        BasicTypes.ShowSizes();
    }
}

The results of running this application are the following: -

Basic type sizes
sizeof short = 2
sizeof int = 4
sizeof long = 8
sizeof bool = 1

Aside from simple built-in types, the sizeof operator can also be used to determine the sizes of other user-created value types such as structs. However, the results of the sizeof operator can sometimes be less than obvious, such as in the following example: -

// Using the sizeof operator.
using System;
struct StructWithNoMembers
{
}
struct StructWithMembers
{
    short s;
    int i;
    long l;
    bool b;
}
struct CompositeStruct
{
    StructWithNoMembers a;
    StructWithMembers b;
    StructWithNoMembers c;
}
class UnSafe2App
{
   unsafe public static void Main()
   {
    Console.WriteLine("\nsizeof StructWithNoMembers structure = {0}",
                      sizeof(StructWithNoMembers));
    Console.WriteLine("\nsizeof StructWithMembers structure = {0}",
                      sizeof(StructWithMembers));
    Console.WriteLine("\nsizeof CompositeStruct structure = {0}",
                      sizeof(CompositeStruct));
   }
}

While you might expect this application to print a value of 0 for the structure with no members (StructWithNoMembers), the value 15 for the structure with four of the basic members (StructWithMembers), and the value 15 for the structure that aggregates the two (CompositeStruct), the actual result follows.

sizeof StructWithNoMembers structure = 1
sizeof StructWithMembers structure = 16
sizeof CompositeStruct structure = 24

The reason for this is padding and structure alignment, which relates to how the compiler lays out a struct in the output file's image. For example, if a struct were three bytes long and the byte alignment were set at four bytes, the compiler would automatically pad it with an extra byte and the sizeof operator would report that the struct is four bytes in length. Therefore, you must take this into consideration when determining the size of a structure in C#.