Convert.ToString() handles null, while ToString() doesn’t.
it will throw a NULL reference exception error. So as good coding practice using convert is always safe.
.toString() can override by users. if the value does not overrriden, value can return type.
Convert.ToString() is standard converting method. if the value is null and you try to Convert.
ToString, you can take an error.
Console.WriteLine(“Some Things”.ToString()); //converts
Console.WriteLine(null.toString()); //error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System; namespace CSharpConsoleExamplesCom { class Program { static void Main(string[] args) { //throws an error string str=null; Console.Write(abc.ToString()); Console.ReadKey(); } } } |
It throws this error: An unhandled exception of type ‘System.NullReferenceException’ occurred in…
Its work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Console.WriteLine(null);//returns null Console.ReadKey(); } } } |