开发者

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

开发者 https://www.devze.com 2023-02-06 03:14 出处:网络
What is the difference bet开发者_StackOverflow社区ween this type casting expressions? What is better?

What is the difference bet开发者_StackOverflow社区ween this type casting expressions? What is better?

// One way
var t:MyClass = MyClass(o);
// Another
var t:MyClass = o as MyClass;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消