So we've seen how implicit conversion works.
But what is explicit conversion.
And that's what we're going to look at in this video.
And therefore we're going to cast one data type into another.
And we're going to cast a data type that can hold more information into a data type that can only hold
less information.
So if we look at any of the given examples like this long here, for example, what we can do is we
can create a new int here.
For example, let me show you here this is explicit conversion.
And here I am going to create an int my int two which I'm just going to assign my long to.
Now this won't work right because my long can hold more information, but I can cast it into an int
by using the int keyword before that.
Now you might wonder why suddenly the error is gone.
And how can this big number suddenly be stored inside of this int two?
Well guess what?
It can't.
Because if we now use the console and we print out my int two, let's try that and actually run our
application.
You can see I have a red key statement here.
So let's run this app and we will see that it only shows a reduced number here.
So what is this number.
It's suddenly a very different number.
It's not my long number.
It's not the int number.
It's like a very different number.
So it just did anything like there was an error in the explicit conversion.
That's because the my long held a way too big number.
Now what if my long didn't hold such a long number?
What if it only held a number that was big enough that it would still fit into a integer?
Now, if we do that, you can see that it actually cannot hold the information.
So now my int, which is the item or the variable that we are actually displaying is holding 1323450,
which is what my long was.
But if we have an even longer my long.
So like this we saw that it didn't work.
So it just comes up with a random number that doesn't really fit together.
So it is the same number each time, but it's not the number that we had inside my long.
So that's something you have to be very careful with.
When you are casting from one type into another, it's your responsibility to make sure that whatever
you are casting, so whatever variable that you're casting into a different data type does actually
only hold a value that is manageable or holdable, so to speak, inside of the range of the data type
that you are converting it to.
So you're casting it too.
So now let's try it with float and double.
Okay.
We saw that we can easily use implicit conversion here.
But now what if we use casting here as well where I'm casting from a double into a float.
So I'm going to use my double and it will be a longer number like so.
And now let me paste it into or like cast it into my float where I'm going to say cast it into a float.
So cast my double into the data type that I am.
So my float is of data type float.
So now I'm casting my double into a float.
So this is again here explicit conversion.
So now let's run this.
Let's take this Console.WriteLine statement and actually display my float.
You can see that the float can only hold eight values so to speak.
Right.
So eight digits and the decimal point.
So it just cuts it off after this three right here.
So after this three this is what it can hold.
It cannot hold any more information like double it.
So there's 123 will just be cut off.
So when you are using explicit conversion you either lose the information if it's too big that you're
trying to cast into a different data type, or you're just going to get a cut off value with, for example,
your floating point numbers.
Here you can also cast from a double into an int.
So I can use explicit conversion where I'm using int my int two for example to be int.
And here I'm going to say my double like so.
So this works as well.
Here I'm declaring int in this line.
And here I'm just using it.
So this here works as well.
This is also explicit conversion slash casting okay.
We are casting from one type to another.
Now you can also convert from a double into an int.
Using explicit conversion.
So therefore let me get rid of all of this and really simplify it.
I'm going to create a new int my int 2 or 3 let's say.
And I'm going to create a double which will be my double three.
And it will just hold the value of 13.5 for example.
Right.
And now I just go ahead and overwrite my int three with whatever my double holds.
Now this by itself won't work right because a double can hold more information than an int, but I can
use explicit conversion slash casting by using the int inside of brackets.
So the data type that my int is holding, and I can convert that double three into an int.
Now what will happen is that everything that is past the decimal point will be cut off.
So if I now use the Console.WriteLine statement here so c w and I'm going to print out my int three,
then I'm going to let me get rid of this statement here I'm going to get 13.
So you see that's because an INT can only hold whole numbers a double can hold more.
But we're just cutting off whatever is after the decimal point by using casting okay.
So that's casting for you and explicit conversion specifically.
And that's it for this video.
See you in the next one.