using System.Collections.Generic;
public static void Main()
TypePatternAndCollections();
NullCheckWithTypePattern();
static void GoodOldTypeCheck() {
object o = new Hero("Wade", "Wilson", "Deadpool", HeroType.FailedExperiment, false);
if(h != null) { Console.WriteLine($"1: o is a Hero and is called {h.HeroName}");}
static void NewTypePattern() {
object o = new Hero("Wade", "Wilson", "Deadpool", HeroType.FailedExperiment, false);
Console.WriteLine($"2: h is of type {h.GetType().Name}");
Console.WriteLine($"2: o is a Hero and is called {h.HeroName}");
static void TypePatternAndCollections() {
IEnumerable<Person> pEnumerable = new Person[] {
new Hero("Wade", "Wilson", "Deadpool", HeroType.FailedExperiment, false)
if(pEnumerable is IReadOnlyList<Person> pList && pList[1] is Hero h) {
Console.WriteLine($"3: o is a Hero and is called {h.HeroName}");
static void NullCheckWithTypePattern() {
object someone = new Hero("Wade", "Wilson", "Deadpool", HeroType.FailedExperiment, false);
if(someone is Hero h && h.Type == HeroType.FailedExperiment) {
Console.WriteLine($"4: Someone is the {h.HeroName} hero and not null");
else { Console.WriteLine("4: Someone is null");}
if(someone is null) { Console.WriteLine("4: Someone is null"); }
enum HeroType { NuclearAccident, FailedExperiment, Alien, Mutant, Technology, Other};
enum HeroTypeCategory { Accident, SuperPowersFromBirth, Other };
enum VoughtEmployeeType { TopManagement, TheSeven, LocalHero, RegularPerson };
record Person(string FirstName, string LastName, int? Age = null, Person? Assistant = null);
record Hero(string FirstName, string LastName, string HeroName, HeroType Type, bool CanFly, Person? Assistant = null) : Person(FirstName, LastName, Assistant: Assistant);