Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
A conversion explicitly converts a numeric value of one type to another type.
Here we have some code that assigns the integer 12
to a variable named whole_number
, and the fractional number 12.34
to a variable named fractional_number
. That decimal point within the numbers is important, because it decides which of C#'s two most common numeric types the number will be treated as.
var wholeNumber = 12;
var fractionalNumber = 12.34;
Console.WriteLine(wholeNumber.GetType());
Console.WriteLine(fractionalNumber.GetType());
- The whole number automatically gets assigned a type of
Int32
, and the number with a decimal point gets a type ofDouble
. - As we saw in the previous video, even a number that's technically an integer will be treated as a
Double
if you include a decimal point.- Let's change the assignment to
wholeNumber
from12
to12.0
. - It's still technically the same number, but the decimal point changes its default type.
- You can see that the type for
wholeNumber
is now alsoDouble
.
- Let's change the assignment to
- Remember that
var
implicitly sets the type of a variable based on the value you initially set it to.- We can also explicitly set the type of our variables.
Let me set wholeNumber
's type to int
... And fractionalNumber
's type to double
:
int wholeNumber = 12.0;
double fractionalNumber = 12.34;
Console.WriteLine(wholeNumber.GetType());
Console.WriteLine(fractionalNumber.GetType());
- But this gives us an error when assigning to
wholeNumber
: "Cannot implicitly convert type 'double' to 'int'." What does that mean? - Well, this really doesn't seem to matter much when assigning
12.0
to an integer variable. But what if we were trying to assign12.5
or3.14
or some other number with a fractional part?int wholeNumber = 12.75
- For an integer variable to hold the
double
value, we have to convert it to an integer. - But what do we do with the fractional part, the .75?
- Does it just not matter? Can we drop it? That seems unlikely. You wouldn't want the fractional dollars dropped from your bank account, for example.
- To protect you from issues like this, C# won't let you assign a
double
value to anint
variable without converting it first. - A conversion explicitly converts a numeric value of one type to another type.
- A conversion shows the compiler that you know this is a
double
value, but you definitely want to convert it to anint
, and are certain that there either is no fractional portion to the number, or that losing the fractional portion won't cause any problems. - You do a conversion by placing parentheses before the value you want to convert, containing the type you want to convert the value to:
int wholeNumber = (int)12.75;
Conversions only work with numeric types. You can't convert from a string
to an int
or an int
to a string
, for example.
wholeNumber = (int)"12";
string myString = (string)12;
Cannot convert type 'string' to 'int'
Cannot convert type 'int' to 'string'
- There are often other ways to convert from one non-numeric type to another, but those are provided by libraries.
- Conversions, on the other hand, are provided by the language itself.
- We'll see how to convert from a string to a numeric type in an upcoming video.
Casting is an explicit conversion. It's required when making a conversion from a more-precise numeric type to a less-precise one, because data might be lost. Converting from a floating-point number to an integer loses all the decimal places, for example, so it requires an explicit cast.
But when you're converting from a less-precise numeric type to a more-precise one, there's no risk of data loss, because the more-precise numeric type will always be able to hold the full value from the less-precise one. For example, when converting from an integer to a floating-point number, you can just add ".0` onto the end. It consumes more computer memory to store it now, but it's still the same value.
When converting from a less-precise numeric type to a more-precise one, C# will do an implicit cast. An implicit cast is a conversion between numeric types that happens automatically, without the need to specify that it should.
- For example, suppose I wanted to change the initial value of
fractionalNumber
from12.34
to12
. - There's no need to write
12.0
. - I can just get rid of the decimal point and all the decimal places:
double fractionalNumber = 12;
- It still works, and the resulting value still has a type of
double
because I declaredfractionalNumber
with adouble
type.- C# does an implicit conversion from the less-precise
int
type to the more-precisedouble
type.
- C# does an implicit conversion from the less-precise
int wholeNumber = (int)12.5;
double fractionalNumber = 12;
Console.WriteLine(wholeNumber.GetType());
Console.WriteLine(fractionalNumber.GetType());
-
0:00
Here we have some code that assigns the integer 12 to
-
0:02
a variable named wholeNumber.
-
0:04
And the fractionalNumber, 12.34, to a variable named fractionalNumber.
-
0:10
That decimal point within the numbers is important because it decides which
-
0:15
of C sharp's two most common numeric types the number will be treated as.
-
0:20
The wholeNumber automatically gets assigned the type of Int32, and
-
0:23
the number with the decimal point gets a type of Double.
-
0:27
As we saw in the previous video, even a number that's technically and
-
0:31
integer will be treated as a double, if you include a decimal point.
-
0:35
Let's change the assignment to wholeNumber from 12 to 12.0.
-
0:41
It's still, technically, the same number, but
-
0:43
the decimal point changes its default type.
-
0:47
You can see that the type for wholeNumber is now also Double.
-
0:52
Remember that the var keyword implicitly sets the type of a variable
-
0:56
based on the value you initially set it to.
-
1:00
We can also explicitly set the type of our variables.
-
1:04
Let me set wholeNumber's type to int, and
-
1:07
fractionalNumber's type to double, save that and run it.
-
1:14
But this gives us an error when assigning to wholeNumber.
-
1:18
Cannot implicitly convert type 'double' to 'int', what does that mean?
-
1:23
Well, this really doesn't seem to matter much when assigning 12.0 to
-
1:27
an integer variable.
-
1:29
But what if we were trying to assign 12.5 or 3.14, or
-
1:32
some other number with a fractional part?
-
1:35
For an integer variable to hold the double value,
-
1:38
we have to convert it to an integer.
-
1:41
But what do we do with the fractional part, the 0.75?
-
1:45
Does it just not matter, can we drop it?
-
1:47
That seems unlikely, you wouldn't want the fractional dollars dropped from
-
1:51
your bank account, for example.
-
1:53
To protect you from issues like this, C sharp won't let you assign a double value
-
1:58
to a int variable without converting it first.
-
2:01
A conversion shows the compiler that you know this is a double value, but
-
2:05
you definitely want to convert it to an int.
-
2:07
It shows you are certain there’s either no fractional portion to the number, or
-
2:12
that losing the fractional portion won’t cause any problems.
-
2:15
You do a conversion by placing parentheses before the value you want to convert,
-
2:20
containing the type you want to convert the value to.
-
2:23
So in this case, we want to convert this double value, 12.75, to an integer.
-
2:29
Let's try saving this and see if it runs now.
-
2:34
Everything works again, this value, 12.75, which is the double by default,
-
2:39
has been converted to an int.
-
2:44
Conversions only work with numeric types.
-
2:47
For example, you can't convert from a string to an int, and
-
2:50
you can't convert from an int to a string.
-
2:54
If I try, I get compile errors.
-
2:56
There are often other ways to convert from one non-numeric type to another, but
-
3:00
those are provided by libraries.
-
3:03
Conversions, on the other hand, are provided by the language itself.
-
3:07
We'll see how to convert from a string to a numeric type in an upcoming video.
-
3:11
Casting is an explicit conversion, it's required when making a conversion from
-
3:16
a more precise numeric type to a less precise one because data might be lost.
-
3:22
Converting from a floating-point number to an integer loses all the decimal places,
-
3:27
for example, so it requires an explicit cast.
-
3:31
But when you're converting from a less precise numeric tied to a more precise
-
3:35
one, there's no risk of data loss.
-
3:37
Because, the most precise,
-
3:38
numeric type will always be able to hold the full value from the less precise one.
-
3:43
For example, when converting from an integer to a floating-point number,
-
3:47
you can just add a 0.0 onto the end.
-
3:50
It consumes more memory to store it now, but it's still the same value.
-
3:55
When converting from a less precise numeric type to a more precise one,
-
3:59
C sharp will do an implicit cast.
-
4:01
An implicit cast is a conversion between numeric types that happens automatically
-
4:06
without the need to specify that it should.
-
4:09
For example, suppose I wanted to change the initial value of fractional number
-
4:14
from 12.34 to just 12.
-
4:17
There's no need to write 12.0,
-
4:21
I can just get rid of the decimal point and all the decimal places.
-
4:25
It still works and the resulting value still has a type of double
-
4:29
because I declared fractional number with a double type.
-
4:33
C sharp did an implicit conversion from the less precise int type
-
4:37
to the more precise double type.
You need to sign up for Treehouse in order to download course files.
Sign up