The evolution of Pattern Matching in C# (from version 6 to 10)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody i'm nick and this i'm going to show you the evolution of pattern matching in c-sharp from version 6 all the way to version 10. now pattern matching is something you usually see more often in more functional languages but c-sharp has made great progress in actually adding a very good pattern matching feature in the language and it only gets better it is a feature that i really wish more people used more often and to be honest i should be using it more often because it is awesome and the research out of this video has actually pushed me to use it more just because of what it brings to the table so let's see where it started where it was and then from every single version all the way to 10 how it's evolved into being an honestly an amazing feature if you like the type of content and you want to see more make sure you subscribe bring the simplification bell to get a later when i upload a new video now before i move on let me tell you about the sponsor of this video checkout.com which is also the company i work for at checkout.com we're building the connected finance businesses deserve with a wide range of payment solutions we have 17 offices around the globe and more than 1500 employees and in this year alone we're looking to hire a thousand more and this is where me as an engineering manager and every other team in checkout needs your help we're looking for the brightest engineers that want to take the next step in their career so if that's you please click the link in the description down below i'm hiring as well go to the career website and if a job description sounds exciting please apply we're looking for engineers from all skill levels to join our teams in london paris berlin portal tallinn new york and san francisco if you're in any of those locations please take a look we really need all the talent we can get so let me show you first what i have i have an abstract class shape and it has an area so we're going to use shapes and inheritance to represent pattern matching because i think that really shows a very easily digestible example of the feature so we have a shape then we create a rectangle out of that and then a circle and we calculate the area for that radius uh diameter it's your usual shape inheritance setup and then in this program.cs i have created three different objects a circle a rectangular which is a rectangle with the same height and width and then i'm putting them in a list and i'm randomly selecting one out of them and this project actually let me show you here is currently running in c sharp 6. so in c shop 6 what you can do if you want to identify what type of shape that thing is for example this random shape is a circle for example then you can use the is operator and what that will do is then it means you can either hard cast it or save cast it like this and take the actual circle and print something about it and if i run this enough times one out of three it will be a circle and it should print the circle with area five so this is c sharp six we didn't have any complicated pattern matching at this time but we could use the is operator in that context but that's pretty much it so come see shop 7 let's see what else we can do now before we had to cast this for it to be a circle in here so if i explicitly call that it would look like this now what i can do is delete that and add a name here so this is certain with this and i created basically a circle variable which is already precasted for me so if this is a circle and let me just ensure that it is here just for the example if i debug my code then you see that i go in here i already have a circle here and i can use it without having to go through this extra casting so this made comparisons like this very very easy and that's the beginning of pattern matching here the pattern we're matching is what type is that specific thing however this doesn't stop here the switch was also able to use that so now i can say i can switch on an object which previously we could not and do something like this i can say case circle cc and then in here i can say something like right line this is a circle with area and then in here i can say c dot area so at this point this variable is a circle it's been automatically matched to that type and i can use it but it doesn't stop there for example if i want to make sure that i'm checking for a square what i would do is case rectangle and then i can say when r dot height equals r dot width now this is a floating point comparison so it has this yellow squiggly line don't worry about it but basically what this means is i can also have specific criteria for the type and its properties because at this point i can use height and width from that type and would only match rectangles will have the same height and width and they will say console.writeline this is a square and then of course you can have your default case and then break into that where it says rightline nothing special about this something like this and now if i run this it's a circle so i'm getting the circle area printed here if i run it again oh that's because i've commented out all the other ones let me just quickly uncomment them out so if i rerun this now this is a square it detected it because height and width are the same if i run it again again square remember this is randomly at this context so that's why it's doing that and then nothing special about this and this would mark the beginning of pattern matching really coming together because this would also influence to a degree how switch expression came to be which is a c sharp eight feature so let's see how that went on so i'm gonna bump on the version to language version 8 so now we are in c-sharp 8 and a few things changed first we can now do the following i can say if i want to check that this is a circle but also check some properties about the circle itself let's say that the area is exactly 50 what i can do is use curly braces here and then it will automatically allow me to say i don't know radius is 10. and this will only match a circle whose radius is 10 and i can add multiple things i can have diameter or area my geometry isn't great so i'm probably going to butcher these numbers but i'm just using random ones you can have a radius of a specific number and an area of a specific number and then that would match that thing meaning you can now shorten specific checks so you don't have to say if this is circle and then in here say you know circle dot radius equals 10 and you don't have to do these types of if checks anymore it's here for you to use and to be honest i've never really seen this feature utilized and it's so so convenient even in my code this can really really be used and it's very very practical and you'll see how 9 and 10 make it even more amazing but let's wait for that then i want to show you how switches change because this is great but with gshop 8 we got switch expressions which is an awesome feature so now what we can do let's say we want to get some shape details out of that so i'm going to say shape details equals and i'm going to say random shape switch and i'm going to go for a switch expression so similar to before if i want to match a circle i can do circle and if i want to use the the actual circle object then i can do this and use it here and say this is a circle so it can be as simple as that and it's very good to always have the discard operator here to catch everything that doesn't match it's basically your default so this is a default it didn't match anything but then if you're looking to not use this variable here you can always just discard it and then it's fine you won't have to use it but if you do want to use it and say this is a circle with area sir dots area then you can do that you can also do the same thing as before you can say rectangle here and do something like this is a square and the way you do that here as well is rec when rec dot height equals rec dot weight so you can still do that sort of thing but what you can also do is let's say you don't care about the actual type but you care about some property let's say the area is 100 well you can certainly do that you can use the curly brace syntax and say area is 100 and then you can you know this area was 100. you can totally mix and match a process with pattern matching in switch expressions and of course you know nothing stops us from just printing it down here so shape details oh what type the shade let me quickly fix that yeah shape details and i can just run this and yeah this is a square it happened to be the square um this is the default didn't match anything so you see how things are starting to come together and this this syntax currently is a bit lackluster because you can't do things like area is more than 100 so you have to be very specific however this was feedback to the c-sharp team and this is what they did with c-sharp nine which i believe this is really where pattern matching came together so so well so let's bump up the version to nine here and see where we are now so this is all still relevant but we are no longer going to use that so firstly we got the not operator meaning i can say if random shape is not a rectangle do something so i can negate the outcome of this check and do something with a shape that is not a rectangle and this can be used in many contexts for example you can totally say if random shape is not null for example this is a proper way of doing a negative null check nowadays but that aside this version also allows us to do things like what if i'm i care about a circle with radius that is more than a specific number so i can say circle where the radius is more than 100 and now i can do that not only i can do that but i can say end less than 200 so i can use the end and the not logical operators add it here to mix and match logic for a specific property but also i can still have multiple ones so if i want the area to be more or equal to a thousand i can totally do that and this is where multiple complicated checks could just come together in that single line very very visible in my opinion very very nice and this is where the switch goes to another level because now these things can be used in the switch expression so let's see what we can do here i'm actually going to just delete all that and still have that at default actually so we don't need to delete the default and here we go let's say i want to check for a circle with an area that is more than 100 but less than 200 how do i do that well first i say circle and then curly braces and then i say area more than 100 and less than 200 and then this can match my magic circle look how awesome that looks you can now have specific criteria in different cases on the same type if you want to like diameter is 100 for some reason you can do that no no one stops you and that does not stop there like if you want to go crazy and have something like called area details here and you want to have a switch expression on the random shape dot area so the actual area itself then you can still do that by doing something like this where you say more or equal to 100 and less or equal to 200 and you can totally have a case that looks like this you know you can do that and then have your default catch-all in case something doesn't match i know this might look confusing to you if you look at this for the first time you're like what's happening here but just think of how much you can do and how it can be mixed and matched and used in both your if checks and also your switch expressions and really this doesn't stop here i'm going to pull two examples from microsoft documentation just to explain how they come together in different areas as well so here's the first method i want to talk about so this is an extension method where it checks whether a character is a letter and for that to happen you have to pattern much on the character and say the character is more or equal to lowercase a and less or equal to z or capital a and capital z and this is how you can use in other contexts as well and actually this doesn't stop here you can also use that with tuples so what you can say in this specific example is let's say you have a tuple which is a group size and a visit date for a group ticket discount that you want to get so you're creating a tool and the group size ends the day of the week of the visiting date and then you can say if less or equal to zero for the group size then the group size must be positive and you're discarding the second parameter of the tuple if you want to check for saturday or sunday to have like a free ticket then you ignore the group size and everyone can come in using that same pattern matching in the tuple property then if your group size is more or equal to 5 and less than 10 on a monday it's 20 and you can see how you can use that the logic and the the end and the not operator and the operator and the discarding to bring this together in a very cohesive way it's a very nice way of showcasing how much power you have now in your hand the last thing i'm going to show you in this video is how c-sharp 10 takes this a step further so previously and i'm not going to move the version to 10 just yet i can go now in the shape and i can add let's say a new shape so like shape in shape sort of property it doesn't have any logic behind this i just want to show nesting in the object i'm using pattern matching on so let's say we have a shape in a shape it could be a circle in a circle and it's a property in here a rectangular rectangular so let's go down here to show you this and say if again random shape is rectangle and i want to check that shape in shape so i say something like shape and shape and then i want to check the area in that shape in the shape so i say area is 100 now obviously this has nested curly braces and i have to go one level deep and as you're going deep you have more and more like of these curly braces it would be nice if i could just do something like this where i can just use the property directly well that's what c shop 10 did so if i go back here and i bump the version all the way to 10 and i go back now i can do that now i can check and have pattern matching inside that object which i could before but directly in that line without having to go through nested curly braces so this is how it was enhanced in c sharp 10. well that is pattern matching for you i probably have missed some bits and you can always check the documentation i'm going to link down below to check the evolution of the feature and how you can use the feature in your own code i highly recommend you take a look like i said in my c shop video it is going a bit more functional in some places and pattern matching is definitely one of those features that you don't want to miss out on it really is very very powerful that's all i had for you for this video thank you very much for watching special thanks to my patreons for making videos possible if you want to support me as well you're going to find it in the description down below leave a like if you like this video subscribe marketer like this and ring the bell as well i'll see you in the next video keep coding
Info
Channel: Nick Chapsas
Views: 7,822
Rating: 4.9607291 out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, clean code, dotnet, pattern matching, c# pattern matching, /net pattern matching, switch expression, The evolution of Pattern Matching in C# from version 6 to 10, the evolution of pattern matching
Id: MzNHMJCyU40
Channel Id: undefined
Length: 16min 7sec (967 seconds)
Published: Mon Oct 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.