tryparse string to int c# code example

Example 1: c sharp tryparse

// Any Parse() function has a TryParse function, which returns
// 'true' on a success and 'false' on a failure. On a succes
// it also saves the conerted value in the second parameter.
string value = "160"
int number;
bool success = Int32.TryParse(value, out number);
// success = true
// number = 160

Example 2: parsing string to int c#

string userInput = Console.ReadLine();
int convert;
Int32.TryParse(userInput, out convert);  // Converting String to int