switch with Pattern Matching (C# 7+)
Pattern matching allows switching on type and other patterns.
object obj = 42;switch (obj){ case int i: Console.WriteLine($"Integer: {i}"); break; case string s: Console.WriteLine($"String: {s}"); break; default: Console.WriteLine("Unknown type"); break;}Run the Application
Section titled “Run the Application”dotnet runResult
Section titled “Result”Integer: 42