C# 7 – Pattern Matching – switch with type pattern

In C# 7 they added Pattern Matching.

One application of this is using the switch statement with the type pattern.

Here’s an example:

void MessageReceiver(IMessage msg)
{
	switch(msg)
	{
		case SwipeRequestMessage swipe:
			InitiateSwipePayment(swipe);
			break;
		case ChipInsertRequestMessage chipInsert:
			InitiateChipInsertPayment(chipInsert);
			break;
	}
}

Leave a Comment