Roadmap › C# Language — Basics to Advanced › Module: Methods and Functional C#
Lessons in this course (38)
26. Pattern matching, tuples and deconstruction
IntermediateMediumLesson 26 of 38
csharp
object shape = new Circle(2);
double area = shape switch
{
Circle { Radius: var r } => Math.PI * r * r, // property pattern
Rectangle(var w, var h) => w * h, // positional pattern
null => throw new ArgumentNullException(nameof(shape)),
_ => 0
};
if (order is { Status: Status.Shipped, Total: > 1000 }) { /* ... */ }
int[] arr = [1, 2, 3];
if (arr is [1, .., var last]) Console.WriteLine(last); // list pattern (C# 11)
(string Name, int Age) GetUser() => ("Asha", 28); // tuple
var (name, age) = GetUser(); // deconstruction