using System.Collections.Generic;
public int? Age {get; set; }
public int? Height {get; set; }
public void PrintTextWithPatternMatching() {
if(this is { Age: null, Height: null}) {
Console.WriteLine($"All properties are empty");
else if(this is { Age: null, Height: {}}) {
Console.WriteLine($"Age is empty, Height: {Height}");
else if(this is { Age: {}, Height: null}) {
Console.WriteLine($"Age: {Age}, Height is empty");
Console.WriteLine($"Age {Age}, Height: {Height}");
public void PrintTextOld() {
if(!Age.HasValue && !Height.HasValue) {
Console.WriteLine($"All properties are empty");
else if(!Age.HasValue && Height.HasValue) {
Console.WriteLine($"Age is empty, Height: {Height}");
else if(Age.HasValue && !Height.HasValue) {
Console.WriteLine($"Age: {Age}, Height is empty");
Console.WriteLine($"Age {Age}, Height: {Height}");
public static void Main()
List<Person> persons = new List<Person> {
new Person() { Age = 20 },
new Person() { Age = null, Height = 160 },
new Person() { Age = 20, Height = 180 }
Console.WriteLine("New pattern matching:");
foreach(var person in persons) {
person.PrintTextWithPatternMatching();
Console.WriteLine("\nOld way:");
foreach(var person in persons) {