ToString和Convert.ToString处理null值

Posted wonderfulviews

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
}

运行结果如下:

 

 

以上是关于ToString和Convert.ToString处理null值的主要内容,如果未能解决你的问题,请参考以下文章