when Guards
Use when to add additional conditions to a case.
int number = 15;switch (number){ case int n when n < 10: Console.WriteLine("Less than 10"); break; case int n when n >= 10 && n <= 20: Console.WriteLine("Between 10 and 20"); break; default: Console.WriteLine("Other"); break;}Run the Application
Section titled “Run the Application”dotnet runResult
Section titled “Result”Between 10 and 20