Is there a simpler way to write this type of code?
while (true) {
int? temp = f();
if (temp is null)
{
break;
}
int x = temp.Value;
// ... use x
}
CodePudding user response:
The pattern match expression itself can be like this:
if (temp is not int x)
{
break;
}
// ... use x
Or you can change the entire loop to be like this:
while (f() is int x)
{
// ... use x
}
CodePudding user response:
int? x1, x2;
while ((x1 = f()) is { })
x2 = x1;
CodePudding user response:
How about...
for (int temp; (temp = f()) != null; ){
// use temp
}