C# Tutorial C# Advanced C# References

C# - sizeof() Operator



The C# sizeof() operator returns the size in bytes by a variable of a given type. It does not work with the variables or instances and accepts the type and returns an int value which represents the size of that type in bytes.

Syntax

//returns the size in bytes by 
//a variable of a given type
sizeof(type)

Return Value

Returns the size in bytes by a variable of a given type.

Example:

The example below shows the usage of sizeof() operator.

using System;

class MyProgram {
  static void Main(string[] args) {
    //displaying size in bytes of various types 
    Console.WriteLine("Size of bool : " + sizeof(bool));
    Console.WriteLine("Size of byte : " + sizeof(byte));
    Console.WriteLine("Size of int : " + sizeof(int));
    Console.WriteLine("Size of long : " + sizeof(long));
    Console.WriteLine("Size of float : " + sizeof(float));
    Console.WriteLine("Size of double : " + sizeof(double));
    Console.WriteLine("Size of decimal : " + sizeof(decimal));
  }
}

The output of the above code will be:

Size of bool : 1
Size of byte : 1
Size of int : 4
Size of long : 8
Size of float : 4
Size of double : 8
Size of decimal : 16

❮ C# - Operators