var user = GetUser();
// Type pattern matching
switch (user) {
case null:
Console.WriteLine("No user found.");
break;
case Manager m when m.Department == Department.Engineering:
Console.WriteLine($"User is a manager in the engineering department and has {m.Reports.Count} direct reports.");
break;
case Manager m when m.Department == Department.Marketing:
Console.WriteLine($"User is a manager in the marketing department and manages {m.CustomerAccounts.Count} customer accounts.");
break;
case Trainee t when t.AgeInYears >= 18:
Console.WriteLine($"User is a trainee and has completed {t.Courses.Count(c => c.IsCompleted)} of their training courses.");
break;
case Trainee t: // This case will only be entered if the one above was not
Console.WriteLine($"User is a junior trainee.");
break;
default:
Console.WriteLine($"User is just a user.");
break;
}
Code snippet taken from "Complete C# Quick Reference - C# 7".