Type, property and relational patterns
Type, property and relational patterns
A pattern tests a value against a shape or condition and may introduce a local variable when it succeeds. value is Type name checks a compatible non-null runtime type and binds it to name. A property pattern such as { Weight: > 0 } checks a non-null input and the specified member. Relational patterns use constant bounds; combine them with and, or, not and parentheses. These are pattern combinators, not ordinary Boolean operators.
Patterns work best for classification and extracting data from known shapes. Keep side effects out of property getters used in patterns: the compiler need not evaluate every test in an intuitive source order. The examples use pure getters and C# 14; recursive property and positional patterns date to C# 8, relational/logical patterns to C# 9 and list patterns to C# 11.
using System;object item = new Parcel("North", 3);if (item is Parcel parcel) Console.WriteLine(parcel.Zone);Console.WriteLine(item is string);record Parcel(string Zone, int Weight);The first pattern both tests the runtime type and exposes a typed local. The object is not a string, so that second test fails. The type pattern does not match null.
Expected output (reasoned, not executed):
NorthFalseCombine nested property and range tests
using System;var delivery = new Delivery(new Address("North"), 5);Console.WriteLine(IsLocalSmall(delivery));Console.WriteLine(IsLocalSmall(new Delivery(new Address("North"), 6)));Console.WriteLine(IsLocalSmall(null));static bool IsLocalSmall(Delivery? delivery) => delivery is { Destination: { Zone: "North" }, Weight: > 0 and <= 5 };record Address(string Zone);record Delivery(Address Destination, int Weight);The nested property pattern requires a non-null delivery and destination, the exact zone text and a weight in 1 through 5. The upper endpoint is included. This is classification, not a guarantee that every delivery object is valid.
Expected output (reasoned, not executed):
TrueFalseFalseMatch positional and list shapes
using System;var point = (X: 0, Y: 4);Console.WriteLine(point is (0, > 0));int[] codes = { 7, 9, 0 };Console.WriteLine(codes is [7, .., 0]);Console.WriteLine(new int[] { 7 } is [7, .., 0]);The positional pattern checks the two tuple components. The list pattern requires a first element 7 and last element 0, with any number of middle elements. A one-element sequence cannot satisfy these two required positions.
Expected output (reasoned, not executed):
TrueTrueFalseCommon mistakes and edge cases
A declaration pattern is not an explicit conversion and does not parse a string into a number. Property patterns reject null; is null tests null without relying on an overloaded equality operator. List patterns need a compatible count/length and indexing shape, not just any enumerable. Prefer ordinary iteration for streams. A positional pattern depends on tuple elements or a suitable Deconstruct method; it does not inspect arbitrary field declaration order.
Practice and next steps
Given record Shipment(string Region, int Boxes), implement IsEligible(Shipment? shipment) for region "East" and 1–8 boxes inclusive. Test null, East/1, East/8, East/9, West/4. Use a single property pattern.
Next: Exhaustive switch and null cases.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Type, property and relational patterns — knowledge check
Apply the lesson concepts to the following scenarios.
What is bound by `item is Parcel p` when it succeeds?
Does a property pattern `{ Weight: > 0 }` match null?
Which weight matches `> 0 and <= 5`?
What can `..` represent in `[7, .., 0]`?
Which expression matches the tuple `(0, 4)`?