0

Possible Duplicate:
Whats the main difference between int.Parse() and Convert.ToInt32

What is better to use int.Parse or Convert.toInt? which one is more error prone and performance vice good?

Community
  • 1
  • 1
maztt
  • 12,278
  • 21
  • 78
  • 153
  • 7
    With **over 200 questions** you should really be aware of the auto-suggest that SO provides as you type your question title by now. Are you sure you couldn't find any similar questions? Because I found many. – BoltClock Jan 26 '11 at 10:41

4 Answers4

3

This is the implementation of Converto.ToInt, according to Reflector :

public static int ToInt32(string value)
{
    if (value == null)
    {
        return 0;
    }
    return int.Parse(value, CultureInfo.CurrentCulture);
}
JYL
  • 8,228
  • 5
  • 39
  • 63
3

The Convert.ToInt32() underneath calls the Int32.Parse. So the only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException.

josliber
  • 43,891
  • 12
  • 98
  • 133
Fraz Sundal
  • 10,288
  • 22
  • 81
  • 132
1

Parse is specifically to parse strings and gives you more control over what format the string may be. Convert.ToInt will handle almost any type that can be converted in the first place. What is more error prone depends on your input and the variations you expect. int.TryParse is robust in that it won't throw an exception, the pitfall is that it is easy to hide the parsing error. This choice depends on what you want to do? Do you want to provide feedback, have detailed parsing error info, use alternative / default values?

0

more error prone int.TryParse :)

Sasha Reminnyi
  • 3,442
  • 2
  • 23
  • 27