0

Can anyone please explain me what is the difference between these two string Type Casting method.

Type 1

int i = 90;
string b = i.ToString();

Type 2

int i = 90;
string b = Convert.ToString(i);

I want to know the major difference of using the two different approach. The 1st one is to handle the null value,we can use 'Convert.ToString(); '. And moreover I get to know that '.ToString()' can be override. Can anyone can explain how. And which one is good to use. Thanks in Advance.

Richa Ankit
  • 33
  • 1
  • 6
  • 2
    http://stackoverflow.com/questions/326060/variable-tostring-vs-convert-tostringvariable http://weblogs.asp.net/jongalloway/36308 –  Aug 04 '15 at 12:42
  • 2
    .ToString() does not handle null value – Mohammad Arshad Alam Aug 04 '15 at 12:42
  • 2
    Neither of those are parsing. Both are *formatting*. – Jon Skeet Aug 04 '15 at 12:43
  • 4
    @Arshad: How do you expect an `int` value to be null? – Jon Skeet Aug 04 '15 at 12:44
  • @Arshad: Well again, ToString isn't parsing, and as this is a call to `int.ToString()`, there's no possibility of it being null... – Jon Skeet Aug 04 '15 at 12:49
  • Also related: https://social.msdn.microsoft.com/Forums/vstudio/en-US/ca084595-8502-4efe-b4be-d52ccb7d63c4/converttostring-vs-tostring?forum=csharpgeneral – MakePeaceGreatAgain Aug 04 '15 at 12:50
  • @JonSkeet, I mean to say in terms of parsing reference types – Mohammad Arshad Alam Aug 04 '15 at 12:52
  • @Arshad: Yet *again*, this isn't parsing - and introducing a comparison with a different method not mentioned in the question doesn't feel terribly useful to me. – Jon Skeet Aug 04 '15 at 12:53
  • Also from [documentation](https://msdn.microsoft.com/en-us/library/x70d7t0y%28v=vs.110%29.aspx); _This implementation is identical to `Int32.ToString()`. It formats value by using the formatting conventions of the current thread culture._ – Soner Gönül Aug 04 '15 at 12:54
  • The amount of characters to type. And for sure the type itself. Its extension vs. method I would say. – C4d Aug 04 '15 at 12:55
  • Thanks for your comments. But I want to know the major difference of using the two different approach. The 1st one is to handle the null value 'Convert.ToString(); ' is good. And moreover I get to know that '.ToString()' can be override. Can anyone can explain how. – Richa Ankit Aug 05 '15 at 05:55

0 Answers0