Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Roshan RathodRank #494 · 5K reputationPosted Jul 21, 2020, 8:43 PM EDT
more details refer this link : https://dotnettutorials.net/lesson/difference-between-convert-tostring-and-tostring-csharp/
Jaimin ShethiyaRank #177 · 13.7K reputationPosted Jul 21, 2020, 9:09 PM EDT
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
Soft CornerRank #1,221 · 1.5K reputationPosted Mar 28, 2011, 12:40 AM EDT
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.
Manikavelu VelayuthamRank #413 · 6K reputationPosted Mar 28, 2011, 12:59 AM EDT
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 CornerRank #1,221 · 1.5K reputationPosted Mar 28, 2011, 1:59 AM EDT
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.)
Suthish NairRank #92 · 22.8K reputationPosted Mar 28, 2011, 3:01 AM EDT
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
VulpesRank #55 · 29.2K reputationPosted Mar 28, 2011, 3:20 AM EDT
Manikavelu VelayuthamRank #413 · 6K reputationPosted Mar 28, 2011, 3:46 AM EDT
Thanks Vulpes.
Really you gave an added value information on this post.
Your reply really rocks in all the forum posts.
Soft CornerRank #1,221 · 1.5K reputationPosted Mar 28, 2011, 3:48 AM EDT
so no need to say its Copy & Pasted
Manikavelu VelayuthamRank #413 · 6K reputationPosted Mar 28, 2011, 3:52 AM EDT
It doesnt mean where you get the information, providing answers for the questions matters. You too really rocks. Thanks once again Shrikant.
Suthish NairRank #92 · 22.8K reputationPosted Mar 28, 2011, 5:21 AM EDT
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.
Ashutosh GuptaRank #805 · 2.9K reputationPosted Jul 21, 2020, 11:24 PM EDT