Friday, October 23, 2020

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

The basic difference between them is the Convert function handles NULLS while i.ToString () does not; it will throw a NULL reference exception error. So as good coding practice using convert is always safe. 

Convert.ToString() return an empty string if the object is null.

ToString it will throw a NULL reference exception error.

Example:

 string i = null;

 Console.Write(i.ToString()); // it throw a NULL reference exception error.

 Console.Write(Convert.ToString(i)); // it return an empty string 



No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...