Type conversions
Back to boxes of different shapes. Copying a value from a small box into a big one needs no questions: everything fits. From a big box into a small one, only with your consent, because part of the value may not fit and get lost.
There is a harder task too. The value "42" is text, not a number, and the compiler rejects the line int n = "42";. Yet data from a user or from a file arrives exactly as text. To compute with it, you have to turn the text into a number. Let's go through all three cases.
Implicit conversion
From int to long and to double a value moves on its own:
int apples = 12;
long many = apples;
double exact = apples;
Console.WriteLine(many); // 12
Console.WriteLine(exact); // 12Let's go through it line by line:
long many = apples;puts anintinto alongbox: anyintvalue fits into it with room to spare.double exact = apples;turns the whole12into the fractional12.0.- Both print lines output
12: adoublewith a zero fractional part prints without.0.
Such a conversion is called implicit: C# performs it without your command, because every int value fits into a long and a double exactly. The other way it does not convert on its own. long to int and double to int are compile error CS0266: the value may not fit.
Explicit cast
When you agree to lose part of the value, write the target type in parentheses before the value: (int)price. This is an explicit cast:
double price = 3.99;
int whole = (int)price;
Console.WriteLine(whole); // 3
Console.WriteLine((int)-3.99); // -3Let's see what came out:
(int)pricedrops the fractional part:3.99becomes3, not4.(int)-3.99gives-3: dropping the fraction moves the number toward zero, not down.
Here the box analogy is inexact. A cast does not squeeze the value in or look for the nearest whole number. It cuts off everything after the point.
Parse, Convert and ToString
C# also has ready-made converters—from a string to a number, from a fraction to a whole number with rounding, and from any value to a string:
int year = int.Parse("2026");
Console.WriteLine(year); // 2026
Console.WriteLine(Convert.ToInt32(2.5)); // 2
Console.WriteLine(Convert.ToInt32(3.5)); // 4
string text = year.ToString();
Console.WriteLine(text + text); // 20262026int.Parse
int.Parse("2026") reads a string of digits and returns the number 2026. If the string holds something other than a whole number, such as "4.5", the program stops with an error—there is an example at the end of the lesson.
Convert.ToInt32
Convert.ToInt32 converts values of many types to int, double among them. Unlike a cast, it rounds: to the nearest whole number, and exactly halfway, to the even one. That is why 2.5 gives 2 and 3.5 gives 4. This kind of rounding is called banker's rounding.
Interviewers like to ask how (int)x differs from Convert.ToInt32(x). The answer: a cast drops the fractional part, while Convert.ToInt32 rounds to the nearest whole number.
ToString
The way back is ToString(): it turns a value into a string. year.ToString() gives the text "2026", so text + text glues two strings rather than adding numbers: the result is 20262026.
Common mistake
int.Parse got a string that holds a word besides the number:
int count = int.Parse("12 apples"); // ✗
Console.WriteLine(count);Unhandled exception. System.FormatException: The input string '12 apples' was not in a correct format.This program compiles: the compiler does not check what is inside a string. The error happens at run time. The program stops at the line with int.Parse and prints an error report. Its first line is shown above. It never reaches Console.WriteLine.
int.Parse accepts only a string with a whole number: a leading sign and spaces at the edges are fine, letters and a point are not.
Key points
- From
inttolonganddoublea value converts implicitly; the other way takes an explicit cast. (int)3.99gives3, and(int)-3.99gives-3: a cast drops the fractional part.Convert.ToInt32rounds to the nearest whole number, and exactly halfway to the even one:2.5gives2.int.Parseturns a string into a number and stops the program if the string is not a whole number;ToString()does the reverse.