Which example is correct in terms of number of parentheses? The C# 12 compiler accepts both.
EXAMPLE 1:
switch ((value, Frozen))
{
case ( >= 0 and <= 9, false):
break;
case (_, false):
break;
case (_, true):
break;
}
EXAMPLE 2:
switch (value, Frozen)
{
case ( >= 0 and <= 9, false):
break;
case (_, false):
break;
case (_, true):
break;
}
There is no difference, so both are correct; but usually redundant (unnecessary) parentheses should be omitted unless they help clarity (especially in complex mathematical expressions, where explicit usage of parentheses are preferable to having to reliably know the operator precedence rules). Parentheses in this context can mean:
but: the presence of multiple comma-delimited terms is what distinguishes between these; a value-tuple usage must have at least 2 terms, precisely to avoid this confusion.
So: