how Int32.TryParse(); works?

It looks through each character of the string and builds up a numeric value in an integer. If all the characters are recognized or an appropriate terminator is found, then the numeric value is stored in the out parameter, and true is returned. If during the parsing an unrecognized character is found or an overflow occurs, then false is returned. The rules for recognizing characters as well as what numeric base is at play is determined by the parsing options passed in and/or the current culture settings of the executing thread.
 
Checking the int.TryParse returns a bool, and if that bool is true, then the parse will commence and the out value is then parsed as the input that you tried to parse.
See :
C#:
            string v = "100";
            bool b = int.TryParse(v, out int i);
            if (b)
                Debug.WriteLine(i);
Notice 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.

Between this post and Skydivers, I think you get the gist. hopefully..
 
The OP was asking how it works, not what it does or how to use it. It is like asking how does a car work, and you reply back with a driver's manual.
 
Agreed, but some people can't help ourselves.

Now our OP gets the best of both worlds, and also has examples to follow. :cool:
 
You are the OP. It's forum terminology for the person who asked the question in the opening post. OP / original poster
 
Back
Top Bottom