
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
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”.
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

Harpreet SinghPosted Mar 7, 2015, 1:27 PM
Humayun Kabir Mamun Thank you
Humayun Kabir MamunPosted Mar 6, 2015, 5:28 AM
Nice...
Harpreet SinghPosted Mar 5, 2015, 5:31 AM
Rajeev Ranjan I am glad you like it :)
Harpreet SinghPosted Mar 5, 2015, 3:06 AM
Sourabh Somani Yeah basic but helpful.. Thanks bro :)
Harpreet SinghPosted Mar 5, 2015, 3:03 AM
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 ChoudharyPosted Mar 5, 2015, 2:40 AM
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 SomaniPosted Mar 5, 2015, 1:11 AM
This is a very basic question for C# interview...
Sourabh SomaniPosted Mar 5, 2015, 1:05 AM
Nicely explained bro :) keep it up...