Type Casting
A free lesson from Swipe Coding · C#, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
Type casting means converting a value from one type to another. C# has two kinds: implicit (widening) casting, which happens automatically when no data can be lost, and explicit (narrowing) casting, which you must write yourself because it risks losing information.
int wholeNumber = 10; double decimalNumber = wholeNumber; // implicit widening Console.WriteLine(decimalNumber); // 10
Going the other way — from a larger or more precise type to a smaller one, like double to int — is called narrowing, and C# requires you to write an explicit cast: the target type in parentheses right before the value. This forces you to acknowledge that some information might be lost.
double price = 9.99; int wholePrice = (int)price; Console.WriteLine(wholePrice); // 9
Precision loss isn't limited to decimals: casting a whole-number type into a smaller one can also lose data if the value doesn't fit. When that happens, the number can "wrap around" into a completely unexpected — even negative — result, which is a common source of subtle bugs.
long big = 3000000000L; int smaller = (int)big; Console.WriteLine(smaller); // overflows to an unexpected, incorrect number
Converting a larger type into a smaller one, such as double to int, is called ____ casting and requires an explicit cast.
What does casting the double 9.99 to an int produce?
Widening a value, such as converting an int to a double, happens automatically in C#.
Constants & Type Casting
10 questions · pass with 60% — just like in the app.
What does the const keyword do when applied to a variable?
Constant names in C# conventionally use PascalCase, like MaxPlayers.
The ____ keyword is used for a value that must never change but is only known once the program starts running.
Why are constants useful in code?
Converting an int to a double happens automatically in C# because no data is lost.
Converting a larger type into a smaller one, such as double to int, is called ____ casting.
What does casting the double 9.99 to an int produce?
Explicit (narrowing) casting requires you to write the target type in parentheses before the value.
Casting a whole-number value that is too large to fit into a smaller type can cause it to ____.
Which of these is an example of implicit (widening) casting?
Keep your streak going in Swipe Coding
Swipe through every level of C# on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →