C# Tutorial C# Advanced C# Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

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.


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

Return Value

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


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