What is the difference between Convert.toString and .toString() method
What is the difference between Convert.toString and .toString() method ?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ashutosh GuptaPosted Jul 22, 2020, 3:24 AM
Jaimin ShethiyaPosted Jul 22, 2020, 1:09 AM
Convert.ToString function handles NULLS
while "someText.ToString()" does not handle the NULLS, it will throw a NULL reference exception error in application. so good practice is always used to the Convert.Tostring function.
refer below link for mor details
https://www.c-sharpcorner.com/blogs/difference-between-converttostring-and-tostring-method1#:~:text=Here%20both%20the%20methods%20are,%22convert%22%20is%20always%20safe.
Thanks
Roshan RathodPosted Jul 22, 2020, 12:43 AM
more details refer this link : https://dotnettutorials.net/lesson/difference-between-convert-tostring-and-tostring-csharp/
Suthish NairPosted Mar 28, 2011, 9:21 AM
Vulpes had another valid point, and mine too..
So Mani, hope you understood why we need to use .ToString() in .Net framework.
Also, instead of mouth shouting refer the fourms rules of this site.
Shrikant, your initial sample is copy paste from msdn. I don't afraid of saying that.
If am doing something wrong, you can too.
If you are a member then you need to follow this websites rules.
Manikavelu VelayuthamPosted Mar 28, 2011, 7:52 AM
It doesnt mean where you get the information, providing answers for the questions matters. You too really rocks. Thanks once again Shrikant.
Soft CornerPosted Mar 28, 2011, 7:48 AM
so no need to say its Copy & Pasted
Manikavelu VelayuthamPosted Mar 28, 2011, 7:46 AM
Thanks Vulpes.
Really you gave an added value information on this post.
Your reply really rocks in all the forum posts.
VulpesPosted Mar 28, 2011, 7:20 AM
Suthish NairPosted Mar 28, 2011, 7:01 AM
Both of the methods do the same thing, but in different way.
.ToString() works on formatting method principles (for more details refer msdn).
Example:
DateTime.Today.ToString("dd-MMM-yyyy"); --> 28-Mar-2011
DateTime.Today.ToString() --> 3/28/2011 12:00:00 AM
This is why it will not accept null, because you cannot format a null value.
Convert.ToString(), converts the specified value to its equivalent string representation.
Convert.ToString(DateTime.Today) --> 3/28/2011 12:00:00 AM
Soft CornerPosted Mar 28, 2011, 5:59 AM
ToString is the major formatting method in the .NET Framework. It converts an object to its string representation so that it is suitable for display - by MSDN
(For information about formatting support in the .NET Framework, see Formatting Types.)
Manikavelu VelayuthamPosted Mar 28, 2011, 4:59 AM
But I just want to know, when to use the .ToString() ?
whats the exact purpose of it ?
If it cant handle null value means, why its there in .NET ?
Soft CornerPosted Mar 28, 2011, 4:40 AM
Just to give an understanding of what the above question means see the below code.
int i =0;
MessageBox.Show(i.ToString());
MessageBox.Show(Convert.ToString(i));
We can convert the integer "i" using "i.ToString()" or "Convert.ToString" so what's the difference.
The basic difference between them is "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.