Drag{ON} Game Dev
Member
- Joined
- Oct 20, 2019
- Messages
- 5
- Programming Experience
- Beginner
Could you please explain how Int32.TryParse(); works please?
Last edited:
string v = "100";
bool b = int.TryParse(v, out int i);
if (b)
Debug.WriteLine(i);
v
is a string format, we will assume we collected from user input. b
will determine if the parse was in fact successful to parse v
as a int, even though its declared type is a string. This string we will assume we received from user input. If the user input can be parsed as and int, b
will be true, and i
will be the parsed value from v
. That's why you use conditional logic or Linq to determine the value of b
, and then use i
because we know it parsed. If the parse fails you wont be accessing i
because b
will be false from the failed parse. In which case you get the user to correct their input and try again.