How do I convert a string to an integer in C#?
See Question&Answers more detail:osIf you're sure it'll parse correctly, use
int.Parse(string)
If you're not, use
int i;
bool success = int.TryParse(string, out i);
Caution! In the case below, i
will equal 0, not 10 after the TryParse
.
int i = 10;
bool failure = int.TryParse("asdf", out i);
This is because TryParse
uses an out parameter, not a ref parameter.