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 datatypes Console.WriteLine("sizeof(int): {0}", sizeof(int)); // 4 Console.WriteLine("sizeof(float): {0}", sizeof(float)); // 4 Console.WriteLine("sizeof(char): {0}", sizeof(char)); // 2 Console.WriteLine("sizeof(double): {0}", sizeof(double)); // 8 Console.WriteLine("sizeof(bool): {0}", sizeof(bool)); // 1 } }
sizeof()
method. Then we print the results to the console.RELATED TAGS
CONTRIBUTOR
View all Courses