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值

相关文章:

  • 2021-07-14
  • 2022-12-23
  • 2022-12-23
  • 2021-06-28
  • 2021-11-28
猜你喜欢
  • 2021-08-08
  • 2022-12-23
  • 2022-12-23
  • 2022-01-04
  • 2022-12-23
相关资源
相似解决方案