Variable Types
This lesson gives an overview of all the types of variables in C# like int, bool, double, char and float and much more!
We'll cover the following...
Integer
An integer is a number that does not have any decimal places. It is a whole number, for example, 1,2,3,4 are all integers. 4.3 is not. If you were to try and place the number 4.3 into an integer, the number would be truncated to 4.
There are further different types in an integer as well. All of them differ in size which we’ll discuss further down the lesson.
Let’s take a look at them one by one.
Float
Floats are floating point numbers with a storage size of 4 bytes, which means that these numbers can hold decimal places. This allows us to store numbers such as “8.344” and “34353.24123”.
Let’s go through different types of floats:
Char
A char is a 16-bit data type. This means that a char can store between U+0000 and U+FFFF. char are commonly used to store text in UTF-16 (Unicode) format. A char can be initialized to hold either a number or a character, but it will store only the Unicode value.
Note: ASCII is a system where a numerical value is assigned to every character you can think of. For a complete conversion chart visit http://ascii-code.com/
Boolean
The bool (boolean) type is a 1-byte data type that is either true or false. A true being any number other than zero and false being zero. The true keyword uses the value 1 to assign true.
Sizes
This table portrays how the variable sizes vary with the variable types.
| C# Alias | Size (bits) | Range |
|---|---|---|
sbyte |
8 | -128 to 127 |
byte |
8 | 0 to 255 |
short |
16 | -32,768 to 32,767 |
ushort |
16 | 0 to 65,535 |
char |
16 | A unicode character of code 0 to 65,535 |
int |
32 | -2,147,483,648 to |