Skip to content

Commit

Permalink
Clarify switch expression use
Browse files Browse the repository at this point in the history
  • Loading branch information
mnadareski committed Oct 27, 2024
1 parent 165896e commit 6220382
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Coding Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,32 @@ This section contains information on code standards regardless of which part of
}
```

- If comparing against values for assignment, try to use a `switch` expression instead.

```c#
As an if-else statement:

int x;
if (value == 1)
x = 0;
else if (value == 2)
x = 1;
else if (value == 3)
x = 2;
else
x = -1;

As a switch statement:

int x = value switch
{
1 => 0,
2 => 1,
3 => 2,
_ => -1,
}
```

- When using a `switch` statement, if all switch cases are single-expression, they can be written in-line. You can also add newlines between cases for segmentation or clarity.If the expressions are too complex, they should not be.

```c#
Expand All @@ -224,6 +250,18 @@ This section contains information on code standards regardless of which part of
}
```

- When using a `switch` expression, cases that lead to the same value can be combined using `or`. This is not required, especially if readability would be sacrificed.

```c#
int x = value switch
{
1 or 2 => 0,
3 or 4 => 1,
5 or 6 => 2,
_ => -1,
}
```

- If any of the switch cases are multi-expression, write all on separate lines. You can also add newlines between cases for segmentation or clarity.

```c#
Expand Down

0 comments on commit 6220382

Please sign in to comment.