I have only see as for casting so far. Is wrapping what you want to see casted in parentheses the same as using as for cast?
This is from the Shops and Utilties last lecture.
I have only see as for casting so far. Is wrapping what you want to see casted in parentheses the same as using as for cast?
This is from the Shops and Utilties last lecture.
Pretty much.
First, you can only cast things to other things if they can be casted to each other. Not all types can be casted to some other types.
So, when you cast with as
and the types can not be casted, the code will just leave the target null
and silently continue
var other = "This won't work";
var one = other as Transform; // strings can't be cast to Transforms
In this case, one
is a transform and after that cast, one
will be null and the code will just continue
However, when you cast with ()
and the types can not be casted, you will get an exception
var other = "This won't work";
var one = (Transform)other; // strings can't be cast to Transforms
This will throw and exception. It may even complain at compile time, so the code won’t even build
This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.