< Prev
Next >

# C# Data Types

C# is a strongly types language, which means each and every variable has a specific data type and there is no concept of a type less variable in C#. Each data type tells us the size, range and the type of value that can be stored in a variable.

In C#, there are about 13 built-in data types, which are also known as value types or primitive data types. These value types are categorized into 4 parts. Let us take a look at these categories.

## Primitive data types are categorized into 4 parts

• integer data types, such as byte, sbyte, short, ushort, int, uint, long, ulong.
• floating-point data types, such as float, double.
• decimal data type, such as decimal.
• character data type, such as char.
• boolean data type, such as bool.

## Maximum and minimum range of values for integer data types

The integer, floating-point and character data types can store a range of values, which is in between a minimum range and a maximum range.

For many of us who like the reasoning behind equations, we will use a formula to calculate a minimum range and a maximum range of values, that can be stored in the integer data types.

1. Minimum Range - It is the minimum value that can be stored in a data type.
2. Maximum Range - It is the maximum value that can be stored in a data type.

• The minimum range for a signed data type(which can store both positive and negative integer values) is calculated by -2(bits-1), while, the minimum range for an unsigned data type(which can store only positive integer values) is 0(zero).

• The maximum range for a signed data type(which can store both positive and negative integer values) is 2(bits-1)-1, where bits = size of a data type, while, the maximum range for an unsigned data type(which can store both positive integer values) is 2(bits)-1, where bits = size of a data type.

For example - The integer data type byte is an unsigned data type. The size of a byte data type is 8 bits. And, as per formula above, the minimum range of value that can be stored in a variable declared as the byte data type is 0, while the maximum range of value is 28-1 i.e. 255. Hence, the range of values for the byte data type(which is unsigned) is 0 to 255.

## Understanding the integer data types

Let's see a table of information about all the integer data, with each of their size, minimum and maximum range.

Integer Data Type Size Range

byte

1 byte = 8 bits 0 to 255

sbyte

1 byte = 8 bits -128 to 127

short

2 bytes = 16 bits -32768 to 32767

ushort

2 bytes = 16 bits 0 to 65535

int

4 bytes = 32 bits –2,147,483,648 to 2,147,483,647

uint

4 bytes = 32 bits 0 to 4,294,967,295

long

8 bytes = 64 bits –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

ulong

8 bytes = 64 bits 0 to 18,446,744,073,709,551,615

## byte

• A variable of byte data type is of size 8 bits, which is equal to 1 byte.
• The integer data type byte is an unsigned data type i.e. it can store only positive integer values. Therefore, as per the formulas discussed a while back, a minimum value a byte variable can store is 0 and a maximum value in a byte variable is 28-1 or 255. Hence, it's range is 0 to 255.

• ## sbyte

• Size of a sbyte variable is 8 bits or 1 bytes.
• The integer data type sbyte is a signed data type i.e. it can store both positive and negative integer values. The minimum value a byte variable can store is -27 or -128 and a maximum value in a byte variable is 27-1 or +127. Hence, it's range is -128 to 127

• ## short

• Size of a short variable is 16 bits or 2 bytes.
• The integer data type short is a signed data type i.e. it can store both positive and negative integer values. A minimum value that can be stored in a short is -215 or -32768 and a maximum is 215-1 or +32767.
So, it's range is -32768 to 0 to 32767.

• ## ushort

• A variable of ushort data type is of size 16 bits or 2 bytes.
• The integer data type ushort is an unsigned data type i.e. it can store only positive integer values. Therefore, as per the formulas discussed a while back, a minimum value a byte variable can store is 0 and a maximum value in a byte variable is 216-1 or 65535. Hence, it's range is 0 to 65535.

• ## int

• Size of an int variable is 32 bits or 4 bytes.
• The integer data type int is a signed data type i.e. it can store both positive and negative integer values. The minimum value that can be stored in an int is -231 or -2, 147, 483, 648 while, the maximum is 231-1 or 2, 147, 483, 647. So, it's range is -2, 147, 483, 648 to +2, 147, 483, 647.

• ## uint

• A variable of uint data type is of size 32 bits or 4 bytes.
• The integer data type uint is an unsigned data type i.e. it can store only positive integer values. Therefore, as per the formulas discussed a while back, a minimum value a byte variable can store is 0 and the maximum is 232-1 or 4,294,967,295. Hence, it's range is 0 to 4,294,967,295.

• ## long

• The size of a long variable is 64 bits or 8 bytes.
• The integer data type int is a signed data type i.e. it can store both positive and negative integer values. The minimum range of a value that can be stored in a long is -263 while, the maximum is 263-1
Note: Because of a wider range than int, the long data type is used to store a larger number.

• ## ulong

• A variable of ulong data type is of size 64 bits or 8 bytes.
• The integer data type ulong is an unsigned data type i.e. it can store only positive integer values. Therefore, as per the formulas discussed a while back, the minimum value a ulong variable can store is 0 and the maximum is 264-1 or 4,294,967,295.

## Understanding the floating-point data types

Let's see a table of information about the floating-point data types, with each of their size, minimum and maximum range.

Floating-Point Data Type Size Range

float

4 byte = 32 bits 1.5E–45 to 3.4E+38.

double

8 byte = 64 bits 5E–324 to 1.7E+308.

## float

• Using float data type, we can hold a number with its fractional part as well. So, we use this data type when a very precise calculation is required.
• Size of a float variable is 32 bits= 4 bytes.
• You don't usually need to remember the minimum and maximum value of float data types but for those who still wish to know, its minimum range is 1.5E–45 and a maximum limit of 3.4E+38.

• ## double

• Size of a double variable is 64 bits= 8 bytes.
• Double data type variable may store a data with a minimum value of 5E–324 until a maximum value of 1.7E+308

• Note: In double data type, we can hold numbers with double precision values, as compared to float.

## Understanding the decimal data type

Let's see a table of information about the decimal data type, with its size, minimum and maximum range.

Data Type Size Range

decimal

16 byte = 128 bits 1E–28 to 7.9E+28.

• The decimal data type uses 128 bits to hold a decimal value and can represent up to 28 decimal places So, we use this data type when a very precise floating-point arithmetic is being performed.
• You don't usually need to remember the minimum and maximum value of float data types but for those who still wish to know, its minimum range is 1E–28 and a maximum limit of 7.9E+28, a lot larger range than float or double.

• ## Understanding the char data type

Let's see a table of information about the char data types, with its size, minimum and maximum range.

Data Type Size Range

char

2 byte = 16 bits 0 to 65535

• C#'s char type variable takes 16 bits = 2 bytes in the memory. Similar to Java, in C#, we have Unicode characters. Unicode characters are represented by unsigned(only positive) 16-bit numbers. Hence, the minimum range is 0 and the maximum range is 216-1. Ranging from 0 to 65535 values.

• Why Unicode characters in Java?

• Because Unicode characters are 16 bits, a wider range than characters in C or C++(where they were just 8 bits or 1 byte). Hence, in C#, a rich international set of characters are easily represented in Unicode.

## Understanding the bool data type

Data Type Value

bool

true or false

For the bool data type in C, there is no range of values. Unlike programming languages like C/C++, a boolean primitive data type in Java cannot take a 0 or 1 value. A boolean in C# can be either true or false.   