Tuesday, 21 April 2015

Difference between tostring() and convert.tostring()

Standard
The basic difference them is Convert.toString(Variable) handles null values even if variable value become null but variable.toString() will not handle null values it will throw a NULL reference exception error.

Convert is always safe as compair to toString().


example :


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 you use Convert.ToString() . then you need to change this line of code ..

Console.Write(abc.ToString());

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

0 comments:

Post a Comment