Understanding C# Pattern Matching from C# 7.0 to C# 10.0

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys my name is karthik and i am from israel automation.com and welcome to another video for c shot for automation testing video series and in this video i'll be talking about c shot patterns and in this video we'll be talking all about the pattern matching in c shot which was introduced all the way in c sharp seven and i'll be discussing until c sharp 10 and all its features so c sharp introduced the pattern matching in c sharp 7.0 version and since then each major c sharp version extends the pattern matching capabilities the following c sharp expressions and statements supports pattern matching like is expression switch statements and switch expression which was introduced in c sharp 8 and it's also extended even further in c sharp 9 and 10. so we'll see all these things one by one in this particular video so before we jump into the c sharp pattern the c sharp pattern actually has got different expressions to match the pattern itself something like declaration and type patterns constant patterns relational and logical patterns property patterns positional patterns var patterns and discard patterns so this discard pattern is something which is kind of discarded from version c sharp 8.0 and now in c sharp 9 it has been completely replaced so that's the cool thing about the discard pattern which will not be talking about we have discarded this discussion itself but these are the expressions that we'll be seeing completely in this particular video so let's jump into all these patterns and you will understand what i really mean for example this is a simple classical switch case statement you might have seen for example in this particular method that you can see we are trying to verify an email domain if it has an gmail.com domain name so if it is the domain which has gmail.com that is a valid email to mine hotmail then it is valid email domain uh if it's something else then you can see that it's going to return you the invalid email domain address something like this so this is one of the switch case statement you can write this in if else as well but i'm using the switch case statement so that you can understand it more easily while we see the actual switch expression something like this so you can see that we have reduced these lines of code into just three lines of code something like this and it is doing exactly the same operation that you are seeing over here so it says that written email which is the string constant switch of the gmail.com or so this is another logical operation that you can do on the expressions like switch expressions and you can verify if gmail.com or hotmail.com then it's a valid email domain if it is something else then it is an invalid domain if there is nothing then it's going to return you the invited email domain pretty much the same thing that you are seeing over here into just three lines of code so this is called as the logical patterns of switch expressions so you can see that we have reduced the number of lines of code much much lesser than compared to the classical way of doing it we can do the same exact things with even more complex types for example a property pattern over here the user is basically a type which is going to have different properties in it and if you want to verify if the user.email contains gmail.com then you return the user or if a user's phone number has the length of i mean greater than 8 then you return the user and if the password contains dollar or hash then return the user so it can fall under any of these categories so the user can have hotmail.com so the first statement will be skipped similarly the length can be uh less than eight then the second statement will be skipped if the third statement has to pass then it should have a dollar or hash in it so you could see that this is exactly the same switch case statement but you can see the different keywords there we have a venn keyword and there is a lambda expression so this is basically like a comparison that you can do and similarly for the user we have created a local variable like user something like this and then we are doing a vin to perform those operation which is cool so this is called as the property pattern and this is even further extended in c-sharp version 10 which we'll be discussing in the last slide of this particular video and then comes the logical pattern with conjecture and patterns so this is like a logical pattern where we are doing something like this like written the user salary switch so you can see that i'm navigating to a specific property of the user type and i'm telling if his salary is less than 10 000 then return 0 if his salary is greater than or equal to 10 000 and so this is the conjecture and while matching the statements and less than 20 000 then return 10 and greater than or equal to 20 000 and less than thirty thousand then written twenty something like that so this is another way of matching the patterns within swiss case statement so this is the another conjecture and patterns and you can also do something like positional patterns so this positional pattern says that over here i have a salary and tax percentage and i'm going to do something like this if my salary is greater than or equal to 10 000 and greater than 10 for the tax percentage then he is a taxpayer category a and if his salary is less than or equal to 20 000 and less than 20 for the tax percentage then he is a taxpayer category b something like that so this is like a hypothetical situations i just created like a method to show you how the positional patterns works but you could just relate how it works with the real time example this is cool so you don't have to write a lot of if conditions or switch cases to perform testing of each and every single property is to be verified over here it is very very simple so this is the positional patterns of switch statement and then var patterns var pattern is another amazing pattern where it can be used along with the is expression if you really wanted to and you can see that we are creating a local var variable like var of email and var of password within the named parameter over here so for the user we have a parameter called as email but i'm also creating a local var variable which i can use within this particular method something like these so you can verify email.contains gmail password.contains dollar on password contains hash then return the user as user so we're doing an as user and then if there is nothing then just return null so this is the way that you could do the pattern matching with var pattern along with is expression if you really wanted to and then constant patterns so this constant pattern is very very helpful if you have worked with any other automation testing tools like selenium where you would like to return a browser driver then probably you can just do something like this so this browser driver type that you are seeing over here in the parameter is basically an enum type and we're just matching it based on the enum type and returning the driver something like this so you can relate the same thing with any of the type it can be a string constant and then you can pause it and then you can return the value that you're looking for as well so that's about the constant pattern and finally in c sharp 10 they introduced what is called as an extended property pattern which helps you match the value within a property of a complex type for example in user we have a address property which is of a type address again that's a different property altogether and if i want to match the city with its name as bangalore then just return me something like a bangalore colon karnataka something like that so that's that's how i'm gonna be doing like a get formatted address in this particular method so this is the all new c sharp 10 feature so in order to use this particular feature you should have c sharp 10 which is available on dotnet 6.0 which is still in preview state you can download it and you can update your project to that and that starts working for you so that's about the extended property patterns so we'll see all these things and understand how things work in this particular video so for that i'm going to switch to my visual studio code ide all right so this is my already pre-set up project which has got all the normal code that you can really expect for example i have something like a model which has got the address for the complex type for the user so you can see this user has got an address type which is going to hold the address something like this and it also has a business logic class which is going to hold all the different methods which we are going to be trying out with different expressions that we just saw on the slide and then it has a program.cs file which is going to hold a user initialization and then it is going to be called within the speaker main method so that we can see what the business logic is going to do for us based on the data that we are passing in from the initialized user so this is a very very super simple c sharp class and the most important thing which i did in this particular project is i'm actually using dotnet 6.0 which is the target framework so make sure that you use it because the constant pattern matching will not work if you don't really have this particular dot net version all right so now let's try running this bigger code and i will show you what i really mean so if i just do dotnet off run this is going to execute the code for you and it's going to output the result which is sitting within our console.writeline over here so all it's returning is a valid email domain because as you can see over here this particular user actually has got gmail.com in his domain but now i'm going to replace this to the switch expression that i was just talking about so in order to work with a switch expression i'm going to write one more method over here so let's write this public string of email domain verifier that's not classical basically and over here i'm gonna pass the email and once again if you start seeing some of the syntaxes coming for me automatically so this is coming from my github co-pilot buddy he's doing all the magics for you over here and you can see that he's going to give you some suggestions which are more sensical but sometimes it gives you some nonsense suggestion as well but yeah we can just leave it what it is returning for us at the moment so you can see that in order to use the switch expression i'm going to do this written of the string email of the switch statements over here and then we are going to start returning the value that we are looking for for example if he is going to give us a gmail.com as the email domain then that's a valid address so you can see that it's automatically telling me all these things based on the email verification that we have written over here so it does all the models for you and it start doing things for you which is cool and if gmail.com or hotmail.com then you can see it's a valid email address or if it is going to be a xxx.com or xom.com something like that then it's going to be an invalid address if there is nothing then you can use this underscore or the discard pattern step that i was talking about you can just do like this and then you can do an lambda expression and then invalid email domain something like this so this is like a default of the switch case statement which returns default value nothing matches that's it this is the only thing which we need to do and that's about the email verifier using the new switch expression with the logical pattern so let me just format this document with c sharp that looks pretty and now if i just go to this particular method and let's try doing this i'm just gonna hide this guy and i'm just gonna do the same exact thing is valid uh email but i'm gonna be using the email domain verifier instead of the classical and let's see if this code works pretty much as expected so i expect the result to return me the valid email domain which is cool and let's say if i return pass this value over here in my domain save it and let's try running it you will see that it's going to return as the invalid email address which is cool so this is the way that we can use the expressions or the switch expressions in c sharp using the normal logical pattern and the next one which i'm going to show you is how we can actually work with a property pattern in order to work with the property pattern all i'm going to do is i'm just going to create all new method over here and i will show you what i really mean about the property patterns so first i'm going to create a method which is going to return me the user from the get user and probably i'm going to take the parameter as the user and i'm gonna write once again the switch statements something like this so written user switch and i'm gonna say if this particular user has got an email which contains the gmail.com then just tell me that this is going to be a valid user something like this again this is a hypothetical method so don't take it that this is the right way of doing the coding but yeah this is just to show you how you could use the switch expression with the property patterns so i'm just going to create a local variable called as user and i'm going to use this when keyword so this when keyword is going to be used to verify when the user dot email contains gmail then written me the user something like this which is cool and it's going to complain me something because the user is something which is already there as a local variable there probably i'm going to change this to users over here so that compliant will be gone uh and then i'm just gonna do one more thing so if the user so you see that it automatically writes if i'm gonna use the hotmail.com there but that's not the one which i'm going to be using so i'm going to say if the user has a phone number which has the length greater than 8 probably then return me this particular user uh if there is nothing just return me null using the default return type something like this so this is going to be for the get user method so i'm going to be using the exact same thing and we'll see if that really works so for doing that uh is valid uh yeah user something like that i'm just gonna do the business logic username uh is it username verify it no sometimes this is misleading by using the github copilot and we forget what we're trying to do all right and then i'm just gonna write a console.writeline and check the user if it's a valid user which is cool it's written code for me as well uh and then i'm gonna do a dot net run that's gonna tell me if it is a valid or not it's gonna be invalid because we don't really have that particular user so you see that it's written me null probably if i just put something more formatted which is going to give me good result but it is not but let's say i'm going to pass something like a cart mcat at gmail dot com and i'm gonna save this and if i try running it this time it is actually giving me uh the user which means it is actually uh giving me that user which i'm looking for right so that then i can probably get its check user or maybe like get user his name so i can just do the username something like this let's try running it and that gives me the name as john which is cool so this is how we can use the property pattern using the switch statement and we have seen the rest of the things on the slide so i'm not going to keep writing the same code again and again rather i'm just going to copy paste some of the code which i have already written and i will show you how it actually works so you can see that this is for the salary and you can see that we are actually using the pattern something like less than ten thousand then return me zero if it is greater than or equal to ten thousand then uh and if it is less than twenty thousand then it's going to be doing something like this so this is about the logical pattern and comparing how it actually works with the switch expression and we can also do the same thing with the positional pattern which i was just talking about on the slide something like this over here you can see that it is going to do the same thing like you're going to verify the salary and the tax percentage using the position and then we'll return the value out from it so all the idea is going to be pretty much exactly the same but the one thing which i wanted to show you this time is going to be the c sharps all new feature which was something that you can use to verify the extended patterns so in order to do that let's do this public string of get formatted address and then i'm not going to be basically giving the address rather you can use the address basically but i'm going to give you uh the user as the parameter and then i'm going to say if so return the user and i'm gonna say and you can see that i'm using the braces over there which means i can use the property of the user straight away then i'm going to say address dot you can see that this is the extended property pattern of c sharp 10. we can use that and you can see that it already is suggesting something for me over here see if city is bangalore and address dot state is karnataka and the address is equal to india then just give me some formatted address something like this which is cool so you can do this as well if you really wanted to um but we don't have a country over here and i'm not going to go with the suggestion with uh the github co-pilot is telling me rather i'm going to write code which i'm looking for so i'm going to create a local variable using the var pattern as city something like this and then i'm gonna say if and see i'm i can call the city over here the local variable and i'm gonna say if the city is equal to bangalore then you just give me all these things over here like user dot street city uh and state something like this which is cool i'm gonna go with the suggestion of the github co-pilot this time and it automatically do other things for me like this like see city if it is mumbai then give me this and if it is chennai then do this and if it is nothing then write an invalid address reduces a lot of my typing github co-pilot is awesome once again all right so let's see if this works so this is the extended property patterns of c sharp 10 and i'm going to go back over here and let's try calling this and see how it actually works so var formatted address is equal to business logic or formatted address uh i think get formatted address and i'm gonna go right here the console.writeline get the user his his address there you go which is cool i'm not unhappy with the suggestion so i think we have an address without bangalore and all those things so probably i'm gonna give the state as uh bangalore i mean the state is not bangla the city with chennai because i'm from chennai and i'm gonna put chennai here state is tamil nadu and the street is made of arkham this is the place where i was living before uh now let's try running it there we go so it gives me that result made welcome chennai tamil nadu awesome so this is the formatted result that you get back while you pass all these little details which is cool all right so this is how we can work with the c-sharps pattern matching with the switch expression is expressions and when and how we can use all these things within our chord so this is a very very awesome feature of c sharp and this keep extending in every major version of c-sharp releasing very important to understand and we can use the exact same thing for the automation testing as well so once again thank you very much for watching this video and you guys have a great day
Info
Channel: Execute Automation
Views: 873
Rating: undefined out of 5
Keywords: C#, selenium, automation testing, pattern matching, logical pattern, var pattern, extended property pattern, visual studio, GitHub copilot, constant pattern, type pattern, is expression, switch expressions, expressions, C# 7, C# 8, C# 9, C# 10, .NET 6, latest version, understanding C#
Id: 7PlV_g1unkg
Channel Id: undefined
Length: 21min 12sec (1272 seconds)
Published: Fri Sep 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.