difference between convert.tostring() and .tostring() in c#

Introduction:

In asp.net, there are different ways to convert value type to string like convert.tostring() and .tostring().

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.

For example:

          string myVar = "" ;

          myVar = null;
          Console.Write(abc.ToString());

This unhandled exception thrown by NullReference.


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


This takes a blank value instead of null.






No comments:

Post a Comment