Built-in types
In the previous lesson the boxes came in two shapes: int for whole numbers and string for text. Programs need other shapes too: for a price with cents, for a yes-or-no answer, for a single letter. For the common cases C# has built-in types—types the language knows on its own, each with a keyword of its own, such as int or bool.
You will need these seven built-in types most often:
int apples = 12;
long people = 8100000000;
double distance = 2.5;
decimal price = 19.99m;
bool isOpen = true;
char grade = 'A';
string city = "Kazan";
Console.WriteLine(apples); // 12
Console.WriteLine(people); // 8100000000
Console.WriteLine(distance); // 2.5
Console.WriteLine(price); // 19.99
Console.WriteLine(isOpen); // True
Console.WriteLine(grade); // A
Console.WriteLine(city); // KazanEach variable is a box of its own shape. Let's go through the types group by group.
Whole numbers
int
int holds whole numbers from a little over minus two billion to a little over plus two billion. You can print the exact limits: int.MinValue and int.MaxValue hold them.
Console.WriteLine(int.MinValue); // -2147483648
Console.WriteLine(int.MaxValue); // 2147483647For counters, ages and quantities you almost always take int.
long
long holds numbers up to 9223372036854775807—that is 19 digits. You need it when two billion is not enough: the population of the Earth, time in milliseconds. To make even a small number in code a long, add the suffix L: 5L.
Here the box-shape analogy falls short: a type also has a limit. The number 8100000000 is whole, but it does not fit into an int box, so the population of the Earth goes into a long.
Fractional numbers
double
A number with a point, such as 2.5, has the type double in C#. In code the fractional part is always set off with a point, never a comma. double suits measurements: distances, weights, temperatures.
Whole and fractional are different box shapes. The line int price = 9.99; does not compile: the compiler does not drop the fractional part silently, and it reports error CS0266.
The comments in the examples show the output on a computer with English regional settings. If the system uses the Russian number format, the first program of this lesson prints 2,5 and 19,99. When it prints a number, the program sets off the fractional part the way the computer's settings say. In code a point stays a point.
decimal
decimal holds up to 28–29 significant digits and keeps the fractional part in decimal notation, as on a receipt. That is why prices and sums of money are kept in decimal. A number for it is written with the suffix m: 19.99m.
Logical type, character and string
bool
bool holds a yes-or-no answer: true or false. Console.WriteLine prints these values with a capital letter: True and False.
char
char holds exactly one character. In code it is written in single quotes: 'A', '7', '?'. 'A' and "A" are different values: the first is a character, the second a string of one character.
string
string holds text of any length, even empty text. In code it is written in double quotes: "Kazan", "".
A string plus a number
The + sign between a string and a value of another type joins them into one string. The value turns into text and is glued onto the string:
int apples = 12;
string text = "Apples: " + apples;
Console.WriteLine(text); // Apples: 12
Console.WriteLine("Open: " + true); // Open: True
Console.WriteLine("Sum: " + 2 + 3); // Sum: 23Let's take the last line apart. + works from left to right, so the string "Sum: " first gets 2 glued on, and then 3. The result is the text Sum: 23, not the sum 5.
Value types
int, long, double, decimal, bool and char are value types. A variable of such a type holds the value itself, so assignment copies it whole: after b = a each variable has its own copy. string is not on this list, yet strings also live separately after b = a: a = "Oslo" does not change b.
Common mistake
The price was declared as a decimal, but the suffix m was left out:
decimal price = 19.99; // ✗
Console.WriteLine(price);error CS0664: Literal of type double cannot be implicitly converted to type 'decimal'; use an 'M' suffix to create a literal of this typeThe program does not compile. A number with a point and no suffix is a double, and it does not go into a decimal box without an explicit mark. The compiler itself suggests the fix: add m to get 19.99m.
Key points
- Whole numbers go into
int(about ±2 billion) andlong(19 digits), fractional ones intodoubleanddecimal. - Money goes into
decimal, and a number for it is written with the suffixm:19.99m. boolholdstrueorfalseand prints with a capital letter;charis one character in single quotes,stringis text in double quotes."Sum: " + 2 + 3givesSum: 23:+with a string glues text from left to right.