String casts in .NET

后端 未结 9 1064
死守一世寂寞
死守一世寂寞 2021-02-15 02:21

Why is there so may ways to convert to a string in .net? The ways I have seen are .ToString, Convert.ToString() and (string). What is the Difference.

9条回答
  •  青春惊慌失措
    2021-02-15 03:20

    Not to nitpick but null is a valid value for a String object. Therefore (string) null does not throw any exceptions. Try it for yourselves:

    using System;
    
    namespace Test
    {
        class Program
        {
            public static void Main(string[] args)
            {
                string s = (string) null;
                Console.WriteLine(s);
            }
        }
    }
    

提交回复
热议问题