What is the difference between int.Parse() and int.TryParse() ?
the int.Parse() and int.TryPrase() methods is used to convert a string representation of number to an integer. In case of the string can’t be converted the int.Parse() throws an exceptions where as int.TryParse() return a bool value, false.string text = "500";
string text2 = "dotnet";
int res1 = int.Parse(text); // Convert value into integer
int res2 = int.Parse(text2); //Throws Exception
int num1;
bool res = int.TryParse(text2, out num1); // res will be false and num1 will be 0
if (res == false)
{
// String is not a number.
}
int num2;
// here condition will be true and num2 will be converted in to integer (500)
if (int.TryParse(text, out num2))
{
// It was assigned.
}
0 comments:
Post a Comment