Hi...
Difference between convert.tostring and .tostring in c# with example?
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
VulpesRank #55 · 29.2K reputationPosted Aug 2, 2014, 12:31 AM EDT
As far as ToString("25") is concerned, it depends on what type of instance it's applied to, whether that's type's ToString method has been overloaded to take a string parameter and (if it has) whether "25" is a meaningful value for that parameter.
In the case of the basic numeric types (int, long, double etc), it again returns the string "25" regardless of the value to which it's applied:
In the case of the string or object types, you'll get a compiler error because there is no overload of ToString which takes a string parameter.
VulpesRank #55 · 29.2K reputationPosted Aug 1, 2014, 10:53 AM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Aug 1, 2014, 9:15 AM EDT
//Returns a null reference exception for str.
string str;
object i = null;
str = i.ToString();
//Returns an empty string for str and does not throw an exception. If you dont
string str;
object i = null;
str = Convert.ToString(i);
Davin MartynRank #285 · 8.8K reputationPosted Aug 1, 2014, 9:18 PM EDT