Skip to content

EN CSharp Patterns

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

Pattern matching

Manual C# · Examples UO

A pattern tests a value and can extract data. C# supports constant, type, property, positional, relational and list patterns, combined with and, or and not. _ is a discard pattern.

Simple example (Example.cs)

Save this example separately as Example.cs. Run it independently of the other examples on this page.

Example.cs:

// PROJECT-EXAMPLE
int value = 15;
System.Console.WriteLine(value is > 10 and < 20);

Result:

True
object value = "backpack";
if (value is string { Length: > 0 } name)
{
    UO.Print(name);
}
int[] numbers = [1, 2, 3];
string shape = numbers switch
{
    [1, .., 3] => "starts with 1 and ends with 3",
    [] => "empty",
    _ => "other"
};
UO.Print(shape);

Official documentation: Pattern Matching - Microsoft Learn.

Clone this wiki locally