直接演员与“作为”操作者?考虑以下代码:void Handler(object o, EventArgs e){
// I swear o is a string
string s = (string)o; // 1
//-OR-
string s = o as string; // 2
// -OR-
string s = o.ToString(); // 3}这三种铸造的区别是什么(好的,第三种不是铸造,但你得到了意图)。哪一种是最好的呢?
3 回答
叮当猫咪
TA贡献1776条经验 获得超12个赞
o
o
(string)o
as
o
s
null
s
:
string s = o as string;if ( s == null ){ // well that's not good! gotoPlanB();}
s
as
ToString
o
ToString
- 3 回答
- 0 关注
- 299 浏览
添加回答
举报
0/150
提交
取消