Is there a difference between "... as class_name" and "(class_name) ..." [duplicate]
I have a problem with the following line of code:
class_name Obj1 = (class_name)something_else;
My boss proposes the following:
class_name Obj1 = something_else as class_name;
I believe this is the same, so this modification is useless. I cannot simply test this.
Can anyone confirm that both are the same and in case not, explain me the difference?
Thanks in advance
Solution 1:
They aren't the same.
If the cast fails, as
will return null
, and the cast expression will throw.