jrliv
5/19/2017 - 1:41 AM

Example using conditional parsing from string to number with the conditional operator.

Example using conditional parsing from string to number with the conditional operator.

int num;
string str = Console.ReadLine();
bool parse = Int32.TryParse(str, out num);

Console.WriteLine(parse ? num + " squared is " + num * num + "." : "Invalid number.");

// 4
// 4 squared is 16.

// test
// Invalid number.