Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
CSharp switch expression with or and default guard with condition

What is the behavior to be expected when using switch expressions with or and a default guard with a condition? I'd expect that anything that qualifies for one of the or statements or for the default guard would enter that case, but apparently this is not how this works.

Consider the following example

var types = new HashSet<string>();
types.Add("a");
types.Add("b");
types.Add("c");

var input = "d";
var result = input switch {
   "d" or _ when types.Contains(input) => "1",
   _ => "2",
};

        
Console.WriteLine(result); //I'd expect "1" but I get "2"
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Documentation states:

The result of a switch expression is the value of the expression of the first switch expression arm whose pattern matches the input expression and whose case guard, if present, evaluates to true

First arm of your expression violates this, because guard is present, and guard evaluates to false. So the result is that of a second arm ("2").

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!