Home / Forums / Pattern Matching Mastery in C# 11 & 12: List Patterns & Property Patterns [Discussion #4]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

Pattern Matching Mastery in C# 11 & 12: List Patterns & Property Patterns [Discussion #4]

EventDriven
Reactive Extensions
MEMBER
Rep: 160
Join Date: Sep 2022
Posts: 6
Thanks: 66
5y ago · Aug 13, 2021 10:03 AM
#1
C# 11 introduced list patterns and slice patterns! Here is how you can parse command arguments or byte sequences elegantly with pattern matching.
EventDriven · Reactive Extensions
Rx.NET, event buses, and asynchronous message queues in C#....
The following users thanked EventDriven for this post:
CppModernist
Modern C++ Advocate
MEMBER
Rep: 379
Join Date: Jan 2020
Posts: 6
Thanks: 90
5y ago · Aug 13, 2021 12:22 PM
#2
List patterns like if (args is ["move", var x, var y]) make parsing input commands read like plain English instead of index checking!
CppModernist · Modern C++ Advocate
Writing idiomatic C++20/23: ranges, modules, std::format, an...
SpanSpecialist
Zero-Copy Memory
MEMBER
Rep: 273
Join Date: Aug 2021
Posts: 7
Thanks: 33
5y ago · Aug 13, 2021 4:22 PM
#3
Combined with switch expressions and relational patterns (e.g. >= 0 and <= 100), pattern matching makes complex state machines super concise.
SpanSpecialist · Zero-Copy Memory
Span<T>, ReadOnlySpan<T>, and stackalloc for fast substring ...