-1

Possible Duplicate:
Difference between Convert.ToInt32(string) and Int32.Parse?

are these three exactly the same in performance? or whats the deal?

Community
  • 1
  • 1
JOE SKEET
  • 7,950
  • 14
  • 48
  • 64
  • 9
    A brief reading of the documentation for each of these will let you know what the difference is. – Esteban Araya Mar 28 '11 at 15:33
  • [Google](http://www.google.lk/search?hl=en&q=difference+between+Int32.Parse+or+Int32.TryParse+or+System.Convert.ToInt32%28%29&aq=f&aqi=&aql=&oq=) it – Damith Mar 28 '11 at 15:41
  • There are [many sites](http://cc.davelozinski.com/c-sharp/convertto-vs-parse) that have already benchmarked the differences. –  Dec 18 '18 at 08:09

3 Answers3

2
  1. Parse v. TryParse
  2. Difference between Convert.ToInt32(string) and Int32.Parse?
Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

Convert.ToInt32 works against object and accepts a range of data-types, of which `string is just one.

int.Parse is optimised for the very common string case, and raises an exception on failure. Useful when we expect an integer.

int.TryParse is like int.Parse, but no exception (false instead); useful for testing for an integer.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

System.Convert check if the source object implements IConvertible and tehn call the proper method, so any object that "know" how to be converted to int can be converted that way. Parse and TryParse starts from the string reperesentation. Try parse returns a boolean signaling that conversion succeeded/failed. Parse trows if conversion fails.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115