且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

之间的差异是和关键字

更新时间:2022-06-10 08:32:51

运营商检查如果一个对象可以转换为特定类型。

is

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

例如:

if (someObject is StringBuilder) ...

运营商尝试将对象强制转换为特定类型,并返回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:

借助转换运算符试图将对象转换为特定类型,并抛出exeption如果失败。

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

例如:

StringBuilder b = (StringBuilder)someObject.