Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between var t:MyClass = MyClass(o); and var t:MyClass = o as MyClass;

What is the difference between this type casting expressions? What is better?

// One way
var t:MyClass = MyClass(o);
// Another
var t:MyClass = o as MyClass;
like image 362
Andrey M. Avatar asked Jan 24 '11 05:01

Andrey M.


Video Answer


1 Answers

Oh, I used to know that one..

Ok, the first one will fail if it can't cast to MyClass, ie you'll end up with an exception flying up your stack.

The second one will never throw, and you'll only end up with a null value if a proper cast can't be made.

I think.

like image 118
Metal Avatar answered Oct 16 '22 07:10

Metal