什么是正常的类型转换,并使用“AS”关键字的区别 [英] What is difference between normal typecasting and using “AS” keyword

查看:101
本文介绍了什么是正常的类型转换,并使用“AS”关键字的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能显示的文件:
  直接铸造VS'为'经营者?
  铸造:(NEWTYPE)与对象为NE​​WTYPE

Possible Duplicates:
Direct casting vs 'as' operator?
Casting: (NewType) vs. Object as NewType

什么是正常的类型转换,并使用AS之间的区别关键字?

What is difference between normal typecasting and using "AS" keyword?

推荐答案

使用将无法正常如果对象是错误的类型,以及所产生的价值将是无效,在一个正常的投会抛出一个InvalidCastException:

Using as will fail gracefully if the object is the wrong type, and the resulting value will be null, where a normal cast would throw an InvalidCastException:

object x = new object();
string y = x as string; // y == null
string z = (string)x; // InvalidCastException

这篇关于什么是正常的类型转换,并使用“AS”关键字的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆