Introduction to C# Delegates and Lambdas (Continued)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Applause] [Music] we have to find out what else we can do with delegates and yeah now it's not that basic anymore now we will discuss some topics which are a little bit more complicated but with before we go into these these topics we will take a look at the inspire because i now downloaded the inspire using my my own uh mobile phone and now it worked this is d and spy you see and in dnspy i can simply open the compiled version of our code so if i take a look here um i can paste in the folder and go to oops wrong one go to bin debug net core app oh we are currently 3.1 we will have to change that to 5.0 in a few minutes because up until now we only used features of c sharp eight but now we will switch uh to c sharp nine in a few minutes but still that works perfectly fine here indian spy as you can see now i have my my application here and if i zoom in a little bit oh here it is program and then [Music] uh we'll take a look at the intermediate language up here you can change the intermediate language and here you see some things i think i didn't i didn't compile yet that's the problem let me recompile i'm sorry i didn't compile yet so i was looking for types which aren't there file reload assemblies and lambdas and delegates and let's see whether something changed main see that one dn spy is so intelligent that it recognizes that we have used um a lambda function here but here the code does look a little bit differently if you take a look it says new math up program ad if i go here you see i just passed ad so the code looks a little bit different but if we switch to intermediate language il here then we will find in reality if we take a look at the main method for instance and if we scroll down a little bit then we will find out that the system calls math op math up let me quickly see if i can find it that's the line 15. it's not that easy to read so it always takes me a while yeah here you can see it this for instance if you take a close line at the intermediate language code then you will see that where was it here that here we are in line 15 and line 15 is that one with the multiplication lambda you see it yeah and if you take a close look then it you will see that it still generates a math or blender but here you see that the code is not inside the main method but it calls it calls a function which has a very very strange name you see that one that is a function name that's really a function name it's very strange it was chosen by c sharp and here we can take a look at these functions you see it yeah you can we can take a look at these functions and we will see that it does an add see that one it does a subtract this one or it does a multiplication see that one so c sharp is just pretending it's just pretending to have these lambda functions but in reality it generates a separate type with separate methods and chooses very very strange names see that one these are the function names these are the delegate names very strange names but we don't have to care and the power of the inspire is if you click to see sharp you don't see the generated code but if you want to dive deep you can switch to il and then you see all the crazy details what the sharp compiler does behind the scenes get it this is what i wanted to show you so that was the proof that writing it like that is exactly the same as writing it like that is exactly the same as writing it like that well the last thing is not really true not completely true but yeah for for now it's it's perfectly fine understand that understand the reason of d and spy do we have a lightning talk or something about dn spy anybody chose i think i have d and spy in my list no well then it was good with that we take a look but but still if you want to do a lightning talk about the inspire now you have seen it maybe it raised your interest choose it because the inspire has a lot of interesting tricks in its toolbox and therefore it would definitely be interesting to watch a lightning talk seven minutes about what you can do with the inspo good questions nice the next thing that we have to understand about delegates is that delegates work very very well with generics now i'm not sure whether you all understand what generics are can you give me an indication if let's do the same exercise again a student from a lower class comes to you blah blah blah asks you what is a generic what would be your answer many people make the generic so complicated but generics are rather simple generics enable you to pass in a type to a function like a parameter to a function let me show you what i mean we can write delegate int combine and i will use a and b just to have a different difference before if i write a combined function not a math operation i can now these are called parameters right we all know that a and b are parameters for this for this function and now with that with generics we can simply add type parameters we cannot only add value parameters but we can make the type a parameter we can say here we get a type t it's typically called t and then wherever we have types we can use the type parameter t t t so now we have a method which has two parameters a and b and the type of the parameters are a parameter to the method too understand what i mean it's like a parameter but a type and therefore question does our add method here fulfill the contract of the combined delegate why they get an integer they get a second integer and they return integer they have three times the same type int if i would change something here float would now the add method fulfill the combine delegate no because we don't have int anymore and if you take a look here this parameter t is used here here here so the combine method says it gets two parameters of the same type and it returns the same type and in our case we now have an int float and that is not a combined method anymore if i change that does the add method now fulfill the combined delegate exactly thumbs up yes it does because it gets a float float float with a string string string fulfill the contract yes of course would a person-person person class fulfill the contract yes it does i think you get the point right so a delegate sorry a generic delegate is just a function where not only the value parameters are parameters but also the type of the parameters are parameters we call them really type parameters this here let me undo the int here that's that's good this here is sorry this here is called a type parameter if we understand that now let's take a look at the calculate and print method let's take a look at that please take a look at this implementation and tell me is there does it really matter for this method whether x and y are of type integer would it change anything if x and y would be floats no if it would be a string no if it would be a person object nope doesn't matter it's always the same why do we have to add a calculate and print method over and over again for any type if we would have a second version of this function which would work with strings don't do that now i will really it's a it's a mistake what i do here if i change that one to string and i change that one to string would it really make sense to write a calculator and print intended calculator to print string it doesn't make sense because the code is exactly the same i understand that you have a question let me quickly fulfill the um the train of thoughts here so let's take a look at the following and now it becomes a little bit complicated i know that but stay with me okay i can add a type parameter here i can add the type parameter here i can add the type parameter here and i can replace the math up with combine with the type parameter here and suddenly we have a combine sorry calculator print method with works which works with any type it works with int it works with float it works with string it works with anything let me prove that let me prove that calculate and print a b x y now they are string so i can say x plus y but if i hover over y it will tell me that i will now concatenate strings calculate and print true true x y x and y we do a binary operation sorry i forgot a comma now it works with boolean perfectly fine let's check out what the compiler really does now the compiler will now try to find out for instance here what is the type of x okay let's take a look x is part of the call to calculate and print calculate and print can be found here now it takes a look at the third parameter combine can it find out what combine is yes of course combine is exactly here now it takes a look at the first parameter because x is the first parameter and it finds out that the first parameter oops it's not defined yet it's t so it walks back and takes a look where is t t is a type parameter here so it takes a look at this call here up here and looks did the comp did the programmer give me a type parameter here no the programmer didn't give us a type parameter so we have to do another step it finds out that the first parameter here also have this t so it takes a look at the first parameter of the call which is here true and now it understands by following this this logical combination it understands that in this case in this call here t the type parameter t has to be a boolean and therefore it knows that x is a boolean y is a boolean but the result has to be a boolean the f is a function which returns boolean and he can really compile everything in a type save manner if i would mix things something like this it wouldn't work because it has no understanding here we tell him that t is an int and here we tell him that t is a boolean and that is an error that can't be true it can't be boolean and integer at the same time that's not that simple is it but it's super powerful it's so powerful because now we can write algorithms which which can be used in a very generic way in a very broad way and modern c sharp code you will find this over and over again if you take a take a look at the code in github of modern libraries you this is how modern c sharp looks like this is how you write modern c sharp code you will find landers generics generic delegates all over the place immediately as when we take a look at asp.net at web apis web applications everything you will always see these generic delegates you will rarely write them but you have to understand them so we had a question right yeah is it possible that i say t should be a part of a number or something like that yes yes that's possible that's possible you can for instance say t must be a value type or t must be a class or t must be a class with a parameter sorry with a constructor without parameters t must implement a certain interface yes you can do that you can do that um i will not do it now okay because it's rather complicated syntax and i have to build a lot of steps but yeah yes it's possible definitely by the way we could definitely pass in the type here as a parameter we could say bull here that's fine that's no problem it's just like before we can say bull here too that's perfectly fine if we want it's not necessary but we can the same thing applies that i told you previously about it depends on what you want to do so in many cases throughout this course it will not be necessary that you write something like this this or that you write something like this in i would say 90 out of 100 cases you will just use types classes things that microsoft built but the problem is if you don't understand what this stuff is all about you cannot read the documentation and you cannot apply these libraries only if you get very very deep if you want to be a professional c-sharp developer then you will probably also write such constructs but if you want to get through this course in many cases you will just be you need to be able to understand these things to read the documentation good i give you another few moments to take a look at the code and maybe you come up with with a questionnaire let's do a small exercise let's start with a new console app and this time we are going to build something which makes a little bit more sense okay than this simple add method and with something where we can apply all the things that we have learned so far good i will save this project and i will check it in later on later on so that you will have it or did you already follow along okay nice so if you want to follow along and i encourage you to do please create a new project again a console app this time we will call it lambdas and collections landers and collections and this time please right click on the project go to properties and change to net5 if you already have it if you don't yeah i can help you overcome this problem but you should because it was your homework to install it dot net additionally okay i'll wait a second until you completed it uh how do you change to uh right click on the project properties application target framework here application target framework did you find it yes if you can't find this setting there is another way too if you are on.net 3.1 you can simply open the cs proj file that's the project file and change that from net core app 3.1 i showed you that last time to net 5.0 that's the same did everybody manage to switch 2.05 no no can i help you okay that would have been your homework but please do it until huh okay a new version of visual studio by the way came out i think yesterday or two days ago it yeah but whenever you have time installed it's a small update it's not a large one okay so if you don't have net 5 running on your computer please do that in the homework uh stick to dotnet 3.1 if you if you can't do that additionally i ask you in the cs proj file and i think you have to do that manually in the cs profile you add the language version preview because we are going to use the latest and greatest c-sharp stuff this is cs proj if you want to know how to open the cs proj file just if you are in the cs file just click on the project and it will open automatically of course you can also use any other editor to open it so i will wait a second these are the two settings which we are going to use get used to them because we will use them over and over and over and again did everybody find the setting and change the settings accordingly nice good good good good good now let's do a little bit of csharp9.net 5 lander and collection stuff the first thing that we are going to do delete everything except of using system in dot net 5 it is no longer necessary to write the main method if we just want to write a bunch of statements we don't need a class we don't need a main method nothing like that maybe you have seen the video that i gave you on flipgrid there i describe this feature it's called top level statements microsoft wants to make c-sharp simpler for very very small demos and this is a very small demo okay good let's create um a small data structure and i've seen in the in the github repository that nobody of you in terms of c-sharp recap has chosen the records feature so let me quickly describe the records feature please do not follow along until i tell you because otherwise you will have to delete a lot of code for instance if you want to maintain a list of let's say heroes okay i can create a class hero of course that's possible and then i can create a bunch of properties maybe string first name and property string last name and maybe a property string hero name and maybe a property boolean can fly something like this okay but that's not really a good solution because we would have to create if we want to write beautiful c sharp code a constructor please don't follow along i will delete all this code in a few moments and then i can pass in some parameters here string first name string last name string hero name and boolean can fly and then i will have to assign all these things first name equals to first name i think you get the point right it's boring and then i will have to remove these setters because they are set in the constructor it's so so boring because i if i want to write a really beautiful class here i would have to do additional things with which i don't want to bother you the point is with net 5 we can simply boom get rid of that we don't need it anymore because now we have records we can write record now you can follow along hero round brackets string first name string last name string hero name bool can't fly bang done a record is just a class it's just a class but all the stuff that i showed you before with the constructor and the properties and all this stuff it's done automatically for you really nice it's a new type of class in.net 5 which saves you a lot of typing work got it question it's basically in the first preview versions of dotnet 5 it was called a data class it's just a class holding a bunch of properties that's a record that's not completely true because it's really a class i could don't follow along i could open the curly braces here and this is now a regular class add methods add properties do whatever you want but you don't have to write the constructor you don't have to write the properties that's done for you okay that's a record so whenever you you need a simple data structure where you just want to store a bunch of strings in state times whatever create a record and you're done saves you a lot of typing got it that's record of course there is much much more to it i did a full day workshop this week at a conference and of course it will be on youtube so if you want to dive very deep into the stuff in a bunch of days i think in two weeks i will have all these videos online it is a six hour course about what's new in c sharp nine party on if you want to take a look at it you can good now let's create a bunch of heroes okay var heroes new hero array let's create a list of hero it's better like that sorry just changed my mind what you need to add in order to create a list of heroes as you need to add a using this is why i just added a list let me show you how i got this using for those of you who are not that familiar with visual studio visual studio shows you that there is an error it doesn't know what list is and then you can press ctrl dot it will open up this nice little light bulb and then it offers you the using and you just click on it or just press enter and it will insert the using automatically got it there is another nice feature in the latest version of visual studio if you just type list of hero the light bulb appears here and you can immediately oops i'm sorry i clicked on the wrong menu item again so if you type here the light bulb appears automatically just click on it and it's done now we can create a list of heroes let's create a hero you can write it like that you don't need to write the type again that's also new in c sharp nine you just say u and c sharp is smart enough to recognize that this is a list of hero so this here must be a hero we don't need to write new hero it's not necessary just write you that's fine of course you could write new hero if you want then let's create a bunch of heroes um what's the name ah that's boring what's the name of of deadpool wade wade wait wait wilson wilson thank you that's deadpool and i don't think he can fly then a hero that i like to be honest i don't know his first name so let's say string empty and string empty and it's homelander and homelander can't fly any fans of the boys here in the room i really like the boys so um and a more classical one bruce wayne batman of course and he can't fly oh sorry of course deadpool cannot fly sorry good good agree fine good you can add a lot of different heroes here if you want the important thing is for our demo we need to have at least one hero who can fly the method that i would like to implement together with you based on delegates and landers is a method which returns a list of hero which is called filter heroes who can fly and it gets a list of hero heroes so i need to write some code that takes a list of heroes and returns only those heroes who can fly i encourage you to follow along although we are going to delete a lot of the code that we are writing now okay we will again use time we'll take our time to walk step by step by step towards a modern c-sharp version of this feature so let's create a result list this is a new list of hero then we will iterate over the list of heroes i hope you know the for each statement var hero in heroes then we do an if hero can't fly if the hero can fly we will add it to the result list hero if not not and at the end we'll just say return result list what do you think does it make sense i think so yep good let's give it a try we will do the following we will just call var result filter heroes who can fly filter heroes who are doesn't work filter heroes who can fly and give it our heroes as a parameter the result will again be a list of heroes because it might happen that multiple heroes can fly you know what i will add another hero who can fly string empty string empty just to get a little bit more interesting code stormfront true it's also a female hero from the boys but you can add superman if you want of course so i will wait for a second until you are you follow the law let's quickly uh combine that var heroes who can fly equals to string.join maybe you know this method maybe not i will show you what it does guess what string.join does it concatenates the strings and separates the results with a comma and a space really convenient method did you know that method yeah some of you did maybe some didn't and then we can just console rightline heroes who can fly good any questions so far did we use so far any lambda methods or functions no did we use generics yes we did take a close look where did we use generics just shout out the line number where did we use generics really exactly line 16 or if i scroll up again here line four list is a generic class and we want to have a list of hero we can't store anything in a list and in our case we give it a type parameter to tell the list that in this case we maintain a list of hero that's a generic class if i take a look at this class at the definition you will see exactly the same syntax that you have seen in the code before t by the way do you know how this works if you want to peek into definition not directly jump to the class but take a quick look into how this is declared you can do that with alt f12 alt f12 will give you a quick look into the class for instance if i go here to filter heroes who can fly alt f12 and it shows you here embedded in your code a quick peek into heroes who can fly that's really convenient if you're using some code of you and you want to quickly look at it because you've forgotten how it works alt f12 you don't lose the focus here does any does everybody who wants to follow along have um this this output and does it work if yeah okay we will quickly take a look you know what um i think we now have the big break right yeah exactly so let's do 15 minutes break and of course i can help you overcome your problems do you want to do the break first and then we'll take a look yeah let's start at the beginning of the lesson we'll take at your code and then we will rebuild that stuff to understand lambdas and delegates in this sample okay good
Info
Channel: Rainer Stropek
Views: 3,434
Rating: undefined out of 5
Keywords:
Id: tDxw_cgAa5c
Channel Id: undefined
Length: 37min 15sec (2235 seconds)
Published: Thu Sep 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.