ToString和Convert.ToString处理null值

http://www.cnblogs.com/qinge/p/5687806.html文章来源

1.Convert.ToString能处理字符串为null的情况。

测试代码如下:

1
2
3
4
5
6
static void Main(string[] args)
{
  string msg = null;
  Console.WriteLine(Convert.ToString(msg));
  Console.ReadKey();
}

运行,没有抛出异常。

2.ToString方法不能处理字符串为null的情况,会抛出异常。

测试代码如下:

1
2
3
4
5
6
7
static void Main(string[] args)
{
  string msg = null;
  //Console.WriteLine(Convert.ToString(msg));
  Console.WriteLine(msg.ToString());
  Console.ReadKey();
}

运行结果如下:

111111
原文地址:https://www.cnblogs.com/whl4835349/p/5887521.html