Tuple Patterns in C# 8.0

Programming C# .NET Core

One of the most awaited features of .NET Core 3.0 is the latest version of C#. In addition to some internal improvements to the compiler, C# ver 8.0 brings a few interesting new features to the language like f.e. the Switch Expressions and also Tuple Patterns.

Tuple Patterns allow matching of more than one value in a single pattern matching expression:


switch (state, transition)
{
case (State.Running, Transition.Suspend):
state = State.Suspended;
break;
case (State.Suspended, Transition.Resume):
state = State.Running;
break;
case (State.Suspended, Transition.Terminate):
state = State.NotRunning;
break;
case (State.NotRunning, Transition.Activate):
state = State.Running;
break;
default:
throw new InvalidOperationException();
}