Skip to content

Positional pattern

Context: The positional pattern uses deconstruction to match an object’s members positionally.

public record Point(int X, int Y);
Point p = new Point(3, 4);
if (p is (3, 4))
{
Console.WriteLine("Point (3,4)");
}
if (p is (var x, var y))
{
Console.WriteLine($"X={x}, Y={y}");
}

Matching coordinate systems: Use positional pattern to match points in a 2D grid or to extract coordinates in a game.

Example: In ray tracing, you can match a ray’s origin and direction using positional patterns.