25 February 2015

Converting String To Integer Properly

If I have a string myNumber = '21' and I want it to convert to an integer, the proper way to do this is to use TryParse like this:

int x = 0;

Int32.TryParse(myNumber, out x);

By using TryParse, it won't throw exceptions if in case your string (text) is non-numeric.

No comments:

Post a Comment