Difference Between ToString() Method and Convert.ToString() Method

C#In the previous tutorial, we have seen how ToString() method works and how to override it.  For built-in types, it works fine but for complex types it will return their type.  There is another method which is pretty much similar to ToString() method and that is Convert.ToString().  But there is a one difference between them.  Convert.ToString() can easily handle null values and will return an empty string, but ToString() method cannot handle null values and will throw a null reference exception.  Example of both ToString() and Convert.ToString() is given below.

using System;
using System.Collections;

namespace Hello_World
{
    class Customer
    {

    }

    class Program
    {
        static void Main(string[] args)
        {
            Customer C1 = null;

            try
            {
                //Convert.ToString() Method
                Console.WriteLine("Conver.ToString() Method Returns: {0}", Convert.ToString(C1));

                //ToString() Method
                Console.WriteLine(C1.ToString());
            }
            catch (Exception ex)
            {

                Console.WriteLine("ToString() Method Returns: {0}", ex.Message);
            }
        }        
    }  
        

}

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •