mercredi 15 juillet 2020

Why are we allowed to use a parameter defined by an "is" expression in the else clause of a conditional operator?

Example:

int a = 3;
int b = 4;

int result = (a + b) is int sum && sum < 100 ? sum : sum - 1;

The expression sum - 1 is valid, and sum is still in scope, even though it's in the else condition. Why is that so? I take it that if the is expression returns false, sum would bear its default value, which for int is 0?

Aucun commentaire:

Enregistrer un commentaire