Converting Between Numbers
Casts and Convert methods.
Why Convert?
Different numeric types can't always mix freely. Sometimes you must convert a value from one type to another.
C# offers implicit conversions, explicit casts, the Convert class, and parsing from text. This lesson covers each.
Implicit Conversion
When the target type can hold every possible value of the source, C# converts implicitly with no extra syntax.
An int fits inside a double, so this assignment is automatic and safe.
int whole = 7;
double real = whole;
Console.WriteLine(real);All lessons in this course
- Numeric Types
- Arithmetic and Precedence
- The Math Class
- Converting Between Numbers