Monday 6 November 2017

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

In this article, We will discuss about what is the difference between Convert.ToString() and .ToString().

Whenever we try to convert any value to String at that time peoples confused about whether we Use Convert.ToString() or .ToString().
But it is always recommended to Use Convert.ToString() because it can handle null values and will not generate any kind of error.
But whenever we use .ToString() at that time there is a chance of Error(NullReferenceException Error) and which can't handle null values.

Example:-
string st=null;
string str=Convert.ToString(st); //It will automatically handle null values
   or
string str1=st.ToString(); //It will not handle null values and will generate NullReferenceException

So due to that reason it is better to Use Convert.ToString().


0 comments:

Post a Comment