In this article, we can discuss the main difference between the ToString() and Convert.ToString in C#
Both the function are used to convert data into the string but Convert.ToString handles the Null values. We can check with the example
Example: ToString()
class Program
{
static void Main(string[] args)
{
string name = null;
Console.WriteLine(name.ToString());
Console.ReadLine();
}
}
In the above example, I am getting the Null exception, Refer to the below image.
But when I’m trying to use Convert.ToString with the Null value that time it’s not throwing any exception
class Program
{
static void Main(string[] args)
{
string name = null;
Console.WriteLine(Convert.ToString(name));
Console.ReadLine();
}
}
So as good coding practice using Convert.ToString is always safe.
Need help?
Read this post again, if you have any confusion or else add your questions in Community