铸件之间和C#的区别? [英] C# difference between casting and as?

查看:83
本文介绍了铸件之间和C#的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:

在C#以下的演员之间的区别是什么?

在C#中,是铸造一个对象或使用关键字之间有什么区别?希望这个代码将说明我的意思...

In C#, is a there difference between casting an object or using the as keyword? Hopefully this code will illustrate what I mean...

String text = "Hello hello";
Object obj = text; 

String originalCast = ((String)obj).ToUpper();
String originalAs = (obj as String).ToUpper();



感谢

Thanks

:)

推荐答案

不会抛出一个 InvalidCastException的。相反,如果转换失败,返回空值(这会给你一个的NullReferenceException 如果 OBJ 在你的例子不是一个字符串)。

as will never throw a InvalidCastException. Instead, it returns null if the cast fails (which would give you a NullReferenceException if obj in your example were not a string).

这篇关于铸件之间和C#的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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