How to get the number of bytes occupied by a variable of a given type in C#

1 Answer

0 votes
using System;

class Program 
{
    static void Main()
    {
        Console.WriteLine(sizeof(sbyte)); // 1
        Console.WriteLine(sizeof(byte)); // 1
        Console.WriteLine(sizeof(short)); // 2
        Console.WriteLine(sizeof(ushort)); // 2
        Console.WriteLine(sizeof(int)); // 4 	
        Console.WriteLine(sizeof(uint)); // 4
        Console.WriteLine(sizeof(long)); // 8
        Console.WriteLine(sizeof(ulong)); // 8
        Console.WriteLine(sizeof(char)); // 2
        Console.WriteLine(sizeof(float)); // 4
        Console.WriteLine(sizeof(double)); // 8
        Console.WriteLine(sizeof(decimal)); // 16
        Console.WriteLine(sizeof(bool)); // 1
    }
}



/*
run

1
1
2
2
4
4
8
8
2
4
8
16
1


*/

 



answered Nov 27, 2020 by avibootz

Related questions

1 answer 59 views
1 answer 120 views
1 answer 194 views
1 answer 84 views
1 answer 86 views
...