This serves as introductory documentation to the pattern matching features introduced in C# 8, which I think are particularly useful.
Deconstructors & Positional Patterns
Adding a deconstructor to your class can be done as below. It must be named Deconstruct and be a public void. Any values we want deconstructed can be set as out parameters, then populated. Here all properties are being deconstructed, but a subset could be deconstructed if desired.
While an if statement would probably be easier to read and maintain in this example, positional patterns enable recursion to be used, which can be very useful.
Property Patterns
I've updated the Student model to add a School property.
I prefer using property patterns to positional patterns as they are much more readable. Here's an example method to check whether a student attends a particular school and has a form tutor who teaches Maths.
publicstaticclassYourClassHere{publicstaticstringDisplayPersonInfo(objectperson){stringresult=personswitch{Studentstudent=>$"Student at {student.School} in Year {student.Year}",Teacherteacher=>$"Teacher of {teacher.Subject}",_=>"Person is not a student or a teacher"};returnresult;}}
The syntax can make it easier to read, and it can be a lot more powerful. You can also define recursive switch patterns.
publicstaticclassYourClassHere{publicstaticstringDisplayPersonInfo(objectperson){stringresult=personswitch{Studentstudent=>studentswitch{_whenstudent.Year<10andstudent.Year>6=>"Student in Key Stage 3",_whenstudent.Year>=10andstudent.Year<=13>=>"Student in Key Stage 4",_=>$"Student in Year {student.Year}"},Teacherteacher=>$"Teacher of {teacher.Subject}",_=>"Person is not a student or a teacher"};returnresult;}}
Tuple Patterns (with Switch Expressions)
You can also use Switch Expressions with Tuples to write even more useful code. For example, you could be creating a game with crafting, combining two items to make another.
publicstaticclassYourClassHere{publicstaticCraftingMaterialGetCraftingMaterial(CraftingMaterialitem1,CraftingMaterialitem2){return(item1,item2)switch{// Match the items in both positions(CraftingMaterial.MountainFlowers,CraftingMaterial.SoulGems)=>CraftingMaterial.DwemerMetal,(CraftingMaterial.SoulGems,CraftingMaterial.MountainFlowers)=>CraftingMaterial.DwemerMetal,(CraftingMaterial.Ore,CraftingMaterial.DwemerMetal)=>CraftingMaterial.Ingots,(CraftingMaterial.DwemerMetal,CraftingMaterial.Ore)=>CraftingMaterial.Ingots,// Handle both items being the same (discard for both, to match any)(_,_)whenitem1==item2=>item1,// Default case (with discard)_=>CraftingMaterial.Unknown};}}
This is lovely and easy to read, as well as very powerful.