as string 和 ToString()的区别

as string 和 ToString的区别
例:
object o1 = "string";
object o2 = 1;

string s1 = o1 as string;//return "string"
string s2 = o2 as string;//return null
string s3 = o2.ToString();//return "1"

注:如果object是null,那么ToString()会抛出一个异常,而as string只是单纯的返回一个null

posted on 2013-02-05 18:57  SasoriCao  阅读(1393)  评论(0编辑  收藏  举报

导航