C# Program to print Size of all Data Types

Notes:

C# Program to print Size of all Data Types:
- Data type indicates type of data
- Data type also indicates the amount of memory to be allocated for a specific type of data

- W.K.T. There are 13 primitive value types available in C# language
1. byte : 1 byte
2. sbyte : 1 byte
3. ushort : 2 bytes
4. short : 2 bytes
5. uint : 4 bytes
6. int : 4 bytes
7. ulong : 8 bytes
8. long : 8 bytes
9. float : 4 bytes
10. double : 8 bytes
11. decimal :16 bytes
12. char : 2 bytes
13. bool : 1 byte

To find size of any data type we take help of sizeof special operator

int sizeof(type of data) :
-returns the amount of memory being allocated for a given type of data in bytes

Example Code:

using System;

namespace SizeofOperatorDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Size of byte = " + sizeof(byte) + " byte");
Console.WriteLine("Size of sbyte = " + sizeof(sbyte) + " byte");
Console.WriteLine("Size of ushort = " + sizeof(ushort) + " bytes");
Console.WriteLine("Size of short = " + sizeof(short) + " bytes");
Console.WriteLine("Size of uint = " + sizeof(uint) + " bytes");
Console.WriteLine("Size of int = " + sizeof(int) + " bytes");
Console.WriteLine("Size of ulong = " + sizeof(ulong) + " bytes");
Console.WriteLine("Size of long = " + sizeof(long) + " bytes");
Console.WriteLine("Size of float = " + sizeof(float) + " bytes");
Console.WriteLine("Size of double = " + sizeof(double) + " bytes");
Console.WriteLine("Size of decimal = " + sizeof(decimal) + " bytes");
Console.WriteLine("Size of char = " + sizeof(char) + " bytes");
Console.WriteLine("Size of bool = " + sizeof(bool) + " byte");

Console.ReadKey();
}
}
}