is 和 as 关键字的区别 [英] Difference between is and as keyword

查看:27
本文介绍了is 和 as 关键字的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请说出C#中isas关键字的区别

Please tell what is the difference between is and as keyword in C#

推荐答案

is

is 运算符检查是否可以将对象强制转换为特定类型.

is

The is operator checks if an object can be cast to a specific type.

示例:

if (someObject is StringBuilder) ...

作为

as 运算符试图将对象强制转换为特定类型,如果失败则返回 null.

as

The as operator attempts to cast an object to a specific type, and returns null if it fails.

示例:

StringBuilder b = someObject as StringBuilder;
if (b != null) ...

<小时>

还相关:


Also related:

强制转换运算符尝试将对象强制转换为特定类型, 并在失败时抛出异常.

The cast operator attempts to cast an object to a specific type, and throws an exeption if it fails.

示例:

StringBuilder b = (StringBuilder)someObject.

这篇关于is 和 as 关键字的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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