The converted number is returned by the Parse function, whereas the TryParse method delivers a boolean value indicating if the conversion succeeded and the converted number in an out parameter. Parse throws an exception if the string isn’t in an acceptable format, whereas TryParse returns false.
namespace Parse_TryParse
{
class Program
{
static int Main(string[] args)
{
string strValue = "100F";
int value;
//Runtime exception due to incorrect int value
value = int.Parse(strValue);
Console.WriteLine(value);
//TryParse to avoid runtime exception
bool result= int.TryParse(strValue, out value);
if (result)
{
Console.WriteLine("Successful");
}
else
{
Console.WriteLine("Fail to parse");
}
return 0;
}
}
}