Difference Between .ToString() and Convert.ToString() in C#

If you declare a string variable and don't assign any value in C#. Then by default it take a blank or null value itself . in this case if u use .ToString() method then our program should throw null reference exception. and in Convert.ToString() our program doesn't throw any exception. bcoz by default it takes a blank value instead of null.

see the code example . u will get a clear view. 

using System;

using System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks; 

namespace csharpExamples

{
  
class Program
   {

     
static void Main(string[] args)
      {

         
string abc = "" ;
          abc =
null;
         
Console.Write(abc.ToString());
         
Console.ReadKey();
      }

 }

And if u use Convert.ToString() . then u need to change this line of code ..

Console.Write(abc.ToString()); to Console.Write(Convert.ToString(abc));

to see the output of above program..

This unhandled   exception thrown by NullReference.
 
 
 
 
 
 I hope this article will be helpful in programming ..
 
 Thanks.
 
Ebook Download
View all
Learn
View all