Recently I’ve written two blog articles about the new C# 8 switch expression. I’ve changed my last blog article to a different indentation – so I’m wondering what version do you prefer?
Updated version with some more alignment options.

The first version of the switch expression is using the usual indentation as used with other C# code.
public (LightState Current, LightState Previous) GetNextLight(
LightState currentLight, LightState previousLight)
=> (currentLight, previousLight) switch
{
(LightState.FlashingYellow, _) => (LightState.Red, currentLight),
(LightState.Red, _) => (LightState.Yellow, currentLight),
(LightState.Yellow, LightState.Red) => (LightState.Green, currentLight),
(LightState.Green, _) => (LightState.FlashingGreen, currentLight),
(LightState.FlashingGreen, _) => (LightState.Yellow, currentLight),
(LightState.Yellow, LightState.GreenBlink) => (LightState.Red, currentLight),
_ => (LightState.FlashingYellow, currentLight)
};
In regard to the number of spaces, I’m using two spaces instead of four to reduce the line length. This makes code in books (and probably in blogs as well) easier to read.
In the second version, I’m using center-based alignment by moving the lambda operator of every single selection to the same position and align the left and right expressions accordingly.
public (LightState Current, LightState Previous) GetNextLight(
LightState currentLight, LightState previousLight)
=> (currentLight, previousLight) switch
{
(LightState.FlashingYellow, _) => (LightState.Red, currentLight),
(LightState.Red, _) => (LightState.Yellow, currentLight),
(LightState.Yellow, LightState.Red) => (LightState.Green, currentLight),
(LightState.Green, _) => (LightState.FlashingGreen, currentLight),
(LightState.FlashingGreen, _) => (LightState.Yellow, currentLight),
(LightState.Yellow, LightState.GreenBlink) => (LightState.Red, currentLight),
_ => (LightState.FlashingYellow, currentLight)
};
Another version that is also used in one sample in the Microsoft docs (most samples use the first version) is left alignment, but the lambda operators are aligned as well:
public (LightState Current, LightState Previous) GetNextLight(
LightState currentLight, LightState previousLight)
=> (currentLight, previousLight) switch
{
(LightState.FlashingYellow, _) => (LightState.Red, currentLight),
(LightState.Red, _) => (LightState.Yellow, currentLight),
(LightState.Yellow, LightState.Red) => (LightState.Green, currentLight),
(LightState.Green, _) => (LightState.FlashingGreen, currentLight),
(LightState.FlashingGreen, _) => (LightState.Yellow, currentLight),
(LightState.Yellow, LightState.GreenBlink) => (LightState.Red, currentLight),
_ => (LightState.FlashingYellow, currentLight)
};
Moving the right side to the next line is another option mentioned in the comments. I’m adding this here. With longer lines, this option can be easier to read.
public (LightState Current, LightState Previous) GetNextLight(
LightState currentLight, LightState previousLight)
=> (currentLight, previousLight) switch
{
(LightState.FlashingYellow, _)
=> (LightState.Red, currentLight),
(LightState.Red, _)
=> (LightState.Yellow, currentLight),
(LightState.Yellow, LightState.Red)
=> (LightState.Green, currentLight),
(LightState.Green, _)
=> (LightState.FlashingGreen, currentLight),
(LightState.FlashingGreen, _)
=> (LightState.Yellow, currentLight),
(LightState.Yellow, LightState.GreenBlink)
=> (LightState.Red, currentLight),
_
=> (LightState.FlashingYellow, currentLight)
};
With the using static directive, the enum type name can be removed accessing the enum members. This makes the lines shorter, and removes the need to split it to two lines. This is an extension to the idea of emmajoey’s comments:
public (LightState Current, LightState Previous) GetNextLight(
LightState currentLight, LightState previousLight)
=> (currentLight, previousLight) switch
{
(FlashingYellow, _) => (Red, currentLight),
(Red, _) => (Yellow, currentLight),
(Yellow, Red) => (Green, currentLight),
(Green, _) => (FlashingGreen, currentLight),
(FlashingGreen, _) => (Yellow, currentLight),
(Yellow, GreenBlink) => (Red, currentLight),
_ => (FlashingYellow, currentLight)
};
Martin Ullrich @dasmulli had the idea of spreadsheet alignment:
public (LightState Current, LightState Previous) GetNextLight(
LightState currentLight, LightState previousLight)
=> (currentLight, previousLight) switch
{
(LightState.FlashingYellow, _) => (LightState.Red, currentLight),
(LightState.Red, _) => (LightState.Yellow, currentLight),
(LightState.Yellow, LightState.Red) => (LightState.Green, currentLight),
(LightState.Green, _) => (LightState.FlashingGreen, currentLight),
(LightState.FlashingGreen, _) => (LightState.Yellow, currentLight),
(LightState.Yellow, LightState.GreenBlink) => (LightState.Red, currentLight),
_ => (LightState.FlashingYellow, currentLight)
};
Using the using static directive, the line length could be reduced with spreadsheet alignment as well:
public (LightState Current, LightState Previous) GetNextLight(
LightState currentLight, LightState previousLight)
=> (currentLight, previousLight) switch
{
(FlashingYellow, _) => (Red, currentLight),
(Red, _) => (Yellow, currentLight),
(Yellow, Red) => (Green, currentLight),
(Green, _) => (FlashingGreen, currentLight),
(FlashingGreen, _) => (Yellow, currentLight),
(Yellow, GreenBlink) => (Red, currentLight),
_ => (FlashingYellow, currentLight)
};
What do you think? Which alignment do you prefer for the switch expression?
Probably a Roslyn extension can be useful to automatically create a preferred indentation.
In the code snippet another position of a different Lambda operator is useful for discussion as well. Do you prefer the lambda operator to start the implementation of a method right at the end of the line of the method declaration, or starting it in the next line as I’ve used it here?
Enjoy learning and programming!
Christian
Links
Changing State with the Switch Expression (C# 8)
Moving from the switch statement to the switch expression (C# 8)
Image choice concepts ID 95308943 © Peshkova | Dreamstime

Leave a comment