The size of a data type is the size it takes in memory. Their sizes vary. This size is represented in bytes. We can use the sizeof()
method to get their sizes.
sizeof(datatype)
datatype
: This is the datatype whose size we want to get in memory.
The value returned is the size of a data type in memory.
using System;class DatatypeSize{static void Main(){// print sizes of some datatypesConsole.WriteLine("sizeof(int): {0}", sizeof(int)); // 4Console.WriteLine("sizeof(float): {0}", sizeof(float)); // 4Console.WriteLine("sizeof(char): {0}", sizeof(char)); // 2Console.WriteLine("sizeof(double): {0}", sizeof(double)); // 8Console.WriteLine("sizeof(bool): {0}", sizeof(bool)); // 1}}
sizeof()
method. Then we print the results to the console.