i have following code.
using system; class program { static void main() { string strnumber = "100tg"; int result = 0; bool isconversionsuccessful = int.tryparse(strnumber, out result); if (isconversionsuccessful) { console.writeline(result); } else { console.writeline("invalid"); } } }
i know tryparse method tries convert strnumber(100tg) integer.
and if succeeds, it's going save converted value result variable , return true boolean. , if fails, result value remain 0 , return false boolean.
my question no matter kind of boolean value isconversionsuccessful variable gets, wouldn't "if(isconversionsuccessful)" activated? misunderstanding tryparse method?
if isconversionsuccessful
gets false
, condition if(isconverstionsuccessful)
evaluates if(false)
. hence, body of if
not execute.
the tryparse
method not determine execution of next line in program. tells whether conversion string
int
successful or not returning boolean value.
the lines following tryparse
you.
Comments
Post a Comment