Welcome back.
And this video, I would like to talk about typecasting and very quickly look at lists, so I have this
function main here.
And this typecasting file in which I create a new variable called string list and it is a list of strings.
So that is the syntax that you can use.
And when you want to create a list, then you can use a list of, for example.
So I'm creating a list of Dennis, Frank, Michael and Gary.
So this is a list which can only contain strings, but you can also create a list which has a mixed
types.
For example, using the any keyboard here, we say that we accept any type of object in our list.
So here we can use strings, then we can use integers, but also doubles and any other data type that
is available.
So let's look at this for loop here.
We have value and mix type list.
So what we do is we go through this mix type list with this fall.
And for every single value in that mix type list, we want to run this, if else, block.
So we check if the value is of type integer.
So we use this is keyword here, is it?
And if it's an end, then please print integer and then the value.
Otherwise, if it's type double, please.
Right, double than the value with the floor value, so we floor the value to its next lower full number.
And otherwise, if it is a string, then please write the string and also write the length of the string.
So just write this print statement and in any other case.
So in this blog, I just say it's an unknown type, so I have no idea of what type it is.
All right.
So this is a follow up.
Alternatively, we could have done it with this for loop here, where instead of using if and else,
if we use went and it works pretty much the same way, only the syntax is different.
So can see we use when the value and then in brackets we say is integer, then execute this code.
So what we have here is a bunch of lambda expressions.
And you can also see once you hover over this if statement that it wants you to use when instead.
So it's pretty much doing the same thing as we have done here and this alternative way.
So both ways are fine.
But Caitlin prefers this one approach.
Now just look at smart casts.
A I smartcast is what you can see here.
So I'm creating this value, which I call object one.
It's a variable which is of type any, and it says I have a dream.
All right.
So I store a string in there, but no check is object one a string, and I'm using the exclamation mark
here.
So this means is it not a string, so is object one and not a string?
Then I'm going to say print another string.
Otherwise, I know that it's a string.
So object is automatically cast to a string in the scope.
So I'm saying found a string of length object at length.
So it already knows OK because it is a string we can use it string functionality, such as getting the
length of their variable.
Then there is something called explicit unsafe casting using the key word.
This can go wrong, by the way.
So we have this other variable called SDR one, which is of type string and what it tries to do is it
tries to use object one as a string.
So this will only work if object one is in fact a string.
If not, then this will go wrong.
So what we're doing is we're using it as a string, we're storing it.
It's an SDR one, and then we print the length.
Then we have this other object here, which is of type any again, and we're storing this time an integer
in there.
And what we're trying is about string string object, a string.
So what we're trying to do is to make a string out of this object to.
And then we're printing it.
As I said, this is unsafe casting, this can go wrong.
And then we can also use explicit essays casting using the ask question my keyword, so here object
three.
Any again, the same value and this case we say, OK, it's a string Nullarbor and Object three as high
strength.
So does it work or does it not work?
Well, in this case, it will work.
So if we use that, it will print.
Now what will happen here?
Well, let's find out.
We can just run the code.
And see, so I'm getting an error here, exception and main thread typecast, Katie 47's on this line
here where I'm saying object to a string.
Why does it go wrong?
Well, because object to is of type any and what is behind it.
It's not a string.
It's an integer.
That's why this call doesn't work.
If I, for example, would go ahead and add the question mark here.
Well, now it's not working because the string is not another string.
So I would have to make this also another string and suddenly it would work.
But that's exactly what we did in this case down here.
All right.
So let's get rid of this line here, because it doesn't add any value.
And let's run it again, and there we are.
So now things work.
Let's start from the top.
We say here at the very top.
Starring Dennis.
Of length.
Five.
OK, so we had this mixed type list, so we go through every single element.
Then the next element is an integer 31.
Then it's an integer five, a string b day with the length of four, then a double seventy point five
with the floor value of seventy.
And then string ways of laying six string kg of length to.
And then again, the same with this second for loop, so.
Exactly the same stuff.
And then we are at this point here where we smartcast.
So.
It sets forth a string of length 14.
And that is this else plot, because we are saying here that I have a dream is not, not a string.
So we go into this book and we print this statement out here.
Then we have this statement where it says object as a string and its length is 14, so object one.
What its object one well is, I have a dream.
So of course, its length will be 14.
And then finally, we have null.
And where does this now come from?
Well, it comes from here where we are trying to save casts and we use Nullarbor.
So our R3 is a Nullarbor, so it can be now.
And we're trying to cast when we are not sure whether it's going to work or not.
And in this case, we were wise to do so because otherwise we would have created an error as we have
done.
And the example with Object two?
All right.
So this is a little introduction to typecasting.
Thanks a lot for your attention.