obj.ToString()、Convert.ToString()、(string)obj、obj as string异同

来源:互联网 发布:移动云商城3.2源码 编辑:程序博客网 时间:2024/06/05 20:42

http://www.cnblogs.com/kenwu/archive/2010/01/10/1643742.html

obj.toString() 如果obj为null 则抛出NullReferenceException 异常;

Convert.toString(obj) obj为空,不会抛出异常而会返回空字符串;

用强制转换 (string)obj 要求 obj 的运行时类型必须是 string。如果不是,就会抛出异常。
用 as 方法则会相对平稳,当 obj 的运行时类型不是 string 时会返回 null 而不抛出异常。

原创粉丝点击