Skip to content
Theory · Variables and types · lesson 7 of 8

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:

C#
int apples = 12;
long many = apples;
double exact = apples;
Console.WriteLine(many); // 12
Console.WriteLine(exact); // 12

Let's go through it line by line:

  1. long many = apples; puts an int into a long box: any int value fits into it with room to spare.
  2. double exact = apples; turns the whole 12 into the fractional 12.0.
  3. Both print lines output 12: a double with 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:

C#
double price = 3.99;
int whole = (int)price;
Console.WriteLine(whole); // 3
Console.WriteLine((int)-3.99); // -3

Let's see what came out:

  1. (int)price drops the fractional part: 3.99 becomes 3, not 4.
  2. (int)-3.99 gives -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:

C#
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); // 20262026

int.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:

C#
int count = int.Parse("12 apples"); // ✗
Console.WriteLine(count);
TEXT
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 int to long and double a value converts implicitly; the other way takes an explicit cast.
  • (int)3.99 gives 3, and (int)-3.99 gives -3: a cast drops the fractional part.
  • Convert.ToInt32 rounds to the nearest whole number, and exactly halfway to the even one: 2.5 gives 2.
  • int.Parse turns a string into a number and stops the program if the string is not a whole number; ToString() does the reverse.