using System;
using static System.Console;
object obj = 7;
switch(obj) {
case string s:
WriteLine($"s = {s}");
break;
case int i when (i % 2 == 0):
WriteLine($"even i = {i}");
break;
case int i when (i % 2 == 1):
WriteLine($"odd i = {i}");
break;
case null:
throw new ArgumentNullException();
default:
WriteLine("unknown type");
break;
}
//prints: "odd i = 7"
| ✓ case 0: — знакомый шаблон константы. ✓ case IEnumerable<int> childSequence: — шаблон типа. ✓ case int n when n > 0: — шаблон типа с дополнительным условием when. ✓ case null: — шаблон NULL. ✓ default: — знакомый вариант по умолчанию. |