
To print this name on the console screen, we can say:
Console.WriteLine(name);

Run the application.

Now let’s say for some reason we passed the value as null for the name field.

But before displaying this name on the console, we need to convert this null into a string and for that we can use Convert.ToString and ToString method.

So, here first we are converting the name field using Convert.ToString and then using ToString method.
Run the application.

The output using Convert.ToString method displays a blank line but output using ToString method throws an un-handled exception.
Difference
Convert.ToString handles null while ToString doesn’t and throws a NULL reference exception.
Note
An instance method ToString cannot be called or used on a null object.
I hope you like it. Thank you
Convert.ToString() vs ToString() in C#
In this article, we will see the difference between Convert.ToString() and ToString().
In Visual Studio, I have a class “Program” with a method “Main”. Inside this main method I have a field called name of type string in which I have assigned a value “Some Name”.


Harpreet SinghPosted Mar 7, 2015, 8:27 AM EST
Humayun Kabir Mamun Thank you
Humayun Kabir MamunPosted Mar 6, 2015, 12:28 AM EST
Nice...
Harpreet SinghPosted Mar 5, 2015, 12:31 AM EST
Rajeev Ranjan I am glad you like it :)
Harpreet SinghPosted Mar 4, 2015, 10:06 PM EST
Sourabh Somani Yeah basic but helpful.. Thanks bro :)
Harpreet SinghPosted Mar 4, 2015, 10:03 PM EST
ToString() is an instance method.So whenever you invoke an instance method on a null object it throws an exception Pankaj Kumar Choudhary
Pankaj Kumar ChoudharyTOP 100Posted Mar 4, 2015, 9:40 PM EST
Can you explain what is reason behind it. Convert.tostring() doesn't throw a Null reference exception but .Tostring() throw an null reference exception ....Thanks
Sourabh SomaniTOP 100Posted Mar 4, 2015, 8:11 PM EST
This is a very basic question for C# interview...
Sourabh SomaniTOP 100Posted Mar 4, 2015, 8:05 PM EST
Nicely explained bro :) keep it up...