3 ms·
C# has boolean pattern-matching (and library functions with TryXYZ names) as a solution to this kind of issue. For instance: while (Console.ReadLine() is s
by victorNicollet 3y ago
C# has boolean pattern-matching (and library functions with TryXYZ names) as a solution to this kind of issue. For instance:
while (Console.ReadLine() is string line &&
int.TryParse(line.Trim(), out int number))
{
Console.WriteLine($"{line} is {number}");
}
This loop can be interrupted by ReadLine() returning null (failing the pattern-matchi as string line) or by int.TryParse returning false.