If a number in a string format you have two options to convert into int or string
1. Parse--Parse() method throws an exception if it can not parse the value
2. TryParse()- TryParse() method returen a boolean whether it is succeeded or failed.
Use Parse() if you are sure value is valid, otherwise use TryParse..
For example- We are using TryParse beacuse our number is invalid.
string num= "100lk";
int result= 0;
bool isConversionsuccessful= int.TryParse(num,out result)
if(isConversionsuccessful)
{
Console.Writeline(result);
}
else
{
Console.Writeline("Please enter valid number");
}
No comments:
Post a Comment
Please let me know if you have any doubts.