本文实例分析了C#中Convert.ToString和ToString的区别,对于初学者来说是很有必要加以熟练掌握的。具体分析如下:
1.Convert.ToString能处理字符串为null的情况。
测试代码如下:
static void Main(string[] args)
{
string msg = null;
Console.WriteLine(Convert.ToString(msg));
Console.ReadKey();
}
运行,没有抛出异常。
2.ToString方法不能处理字符串为null的情况,会抛出异常。
测试代码如下:
static void Main(string[] args)
{
string msg = null;
//Console.WriteLine(Convert.ToString(msg));
Console.WriteLine(msg.ToString());
Console.ReadKey();
}
运行结果如下:
本文地址:https://www.stayed.cn/item/12599
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我