Skip to main content

How to Match Several Patterns with or in C#

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

'a' or 'e' or 'i' or 'o' or 'u' => "a vowel" is one switch arm that matches any of five letters. The or keyword joins patterns, and the value matches when any one of them matches.

Microsoft's patterns reference defines it as an or pattern that matches an expression when either pattern matches the expression. It's one of the logical patterns, next to and and not, and it works in a switch arm and after is. The C# guide says when to reach for it: Choose or when several pattern alternatives have the same result.

Syntax​

Syntax
<pattern1> or <pattern2> or <pattern3>

<value> is <pattern1> or <pattern2>

<value> switch
{
<pattern1> or <pattern2> => <result>,
_ => <otherResult>,
}
PartWhat it is
<pattern1> or <pattern2>Matches when either pattern matches. You can chain as many as you need
<value> is ...Tests one value against the patterns and gives true or false
... => <result>In a switch, one arm with one result for every pattern in the chain

Each pattern in the chain can be any pattern: a constant such as 'a', or a comparison such as < 0.

The Example​

A console app called OrPattern sorts four letters into vowels and consonants with one switch arm for all five vowels, then checks a grade with is and or.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n OrPattern
cd OrPattern
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/OrPattern/OrPattern.csproj:
Determining projects to restore...
Restored /Users/you/source/OrPattern/OrPattern.csproj (in 42 ms).
Restore succeeded.

dotnet new console -n OrPattern creates a folder named OrPattern with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or OrPattern.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the letters:

Program.cs (the switch)
char[] letters = ['a', 'b', 'e', 'x'];

foreach (char letter in letters)
{
// One arm matches any of five letters.
string kind = letter switch
{
'a' or 'e' or 'i' or 'o' or 'u' => "a vowel",
_ => "a consonant",
};

Console.WriteLine($"{letter} is {kind}");
}
  • 'a' or 'e' or 'i' or 'o' or 'u' is one pattern made of five constant patterns. It matches when the letter is any of them, and then the arm gives "a vowel".
  • _ matches every other letter.

Then the same idea with is:

Program.cs (the is check)
// or works after is, too.
char grade = 'B';

if (grade is 'A' or 'B')
{
Console.WriteLine($"{grade} is a top grade");
}

grade is 'A' or 'B' is true when grade is either letter. You write grade once, then the patterns.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
a is a vowel
b is a consonant
e is a vowel
x is a consonant
B is a top grade

macOS-style terminal window titled Terminal — OrPattern showing dotnet run printing a is a vowel, b is a consonant, e is a vowel, x is a consonant and B is a top grade

a and e matched the or arm, and b and x fell through to _. B matched the second pattern in 'A' or 'B', so the if ran.

The Complete File​

macOS-style code window titled Program.cs — OrPattern showing an array of four letters, a loop with a switch expression whose first arm matches a, e, i, o or u joined by or and whose discard arm gives a consonant, and an if that checks whether grade is A or B

Program.cs
char[] letters = ['a', 'b', 'e', 'x'];

foreach (char letter in letters)
{
// One arm matches any of five letters.
string kind = letter switch
{
'a' or 'e' or 'i' or 'o' or 'u' => "a vowel",
_ => "a consonant",
};

Console.WriteLine($"{letter} is {kind}");
}

// or works after is, too.
char grade = 'B';

if (grade is 'A' or 'B')
{
Console.WriteLine($"{grade} is a top grade");
}

Common Mistakes​

MistakeWhat happensDo this instead
Joining the values with || in a switch arm, as in 'a' || 'e' => "a vowel",error CS0019: Operator '||' cannot be applied to operands of type 'char' and 'char'Write 'a' or 'e'. The C# guide: Pattern operators form patterns rather than Boolean expressions
Writing grade is 'A' || 'B'error CS0019: Operator '||' cannot be applied to operands of type 'bool' and 'char'Write grade is 'A' or 'B'. The || operator joins two true or false values, not two patterns
Putting not before an or without parentheses, as in letter is not 'a' or 'e'The build warned warning CS9336: The pattern is redundant., and e gave True, because not applied only to 'a'Put the alternatives in parentheses: letter is not ('a' or 'e') gave False for both letters. The reference: The not pattern binds to its operand first.
Declaring a variable inside an or pattern, as in value is int number or long numbererror CS8780: A variable may not be declared within a 'not' or 'or' pattern.Test each type on its own. Microsoft's error reference: variables in or patterns are only assigned in one branch.

FAQ​

Is or the same as ||?​

For the same values it gives the same answers: grade is 'A' or 'B' and grade == 'A' || grade == 'B' were both True for A and B, and both False for C. What differs is what they join. The C# guide: Pattern operators form patterns rather than Boolean expressions: and corresponds to pattern conjunction, or to pattern alternatives, and not to pattern negation.

Can I join other kinds of patterns with or?​

Yes. temperature is < 0 or > 30 joins two comparisons: it was True for -5 and 35, and False for 12.

What if a later arm repeats a value?​

The build fails. An arm 'e' => "the letter e" after the vowel arm gave error CS8510: The pattern is unreachable. It has already been handled by a previous arm of the switch expression or it is impossible to match. The reference: The switch expression arms are evaluated in text order. With the 'e' arm first, it built, and e printed the letter e while a was still a vowel.

Conclusion​

Join patterns with or when several values should get the same result, so one switch arm or one is check covers them all. Use or between patterns and || between true or false expressions, and put parentheses around the alternatives when not comes before them.

Additional Resources​