C# Why Delegates

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
a question I get a lot when I teach delegates especially when I show a simple example like this is why not just call foo directly making this delegate a reference or references foo and then you invoke it why just replace all this with a foo it it seems pointless to do on that extra work and that's a valid question in this situation this this contrived example if you would this is pretty much useless the thing about these delegates is that they allow us to parameterize code if I make a static int square method that takes an X here and let's just return x times X like so well the reason we want this to be parameterised as I can pass any value here I can pass five I can pass 20 I can pass 13 doesn't matter it's parameterised and then I can square any any value just by passing it in it's this this is the part that flexes at the it's the part that changes well code allows us or Co delegates allow us to pass code here to parameterize our code in a way so instead of just parameterizing integers values or string values or maybe even class types now I can pass code or at least a reference to some code because that's what delegates are there references off to some code in this case I'm referencing foo okay so let me give you an example that will hopefully add some more motivation to why delegates are good I'm gonna just delete all this for now and I'm gonna make a function here static ienumerable enumerable int get all numbers less than 5 that's a pretty lengthy method name but we'll roll with it I ain't in the ball and numbers and never we're gonna say for each in number in numbers if number less than we said less than 5 yeah all the return number all right so now I can I can prove that this works I can say well do I never bowl int result gets get all numbers less than five knees do them out get us some screen analysts eight here hopefully you have it tuned up to HD so you can see all this get all numbers less than five new array and I'm just gonna start pounding my keyboard here randomly that should be good okay get all numbers less than five and then that's for each over the the result and print n so hopefully this is this isn't too nerdy if you're not familiar with the for each or the yield a look at the videos on that and here I have implicitly time to raid the watch the videos for that if you're not up to snuff as to what these constructs are doing but basically get numbers less than five basically runs through every number and if the number is less than 5 then it yields it okay so it looks like 2 is less than 5 3 is less than 5 and one's less than 5 so the rest of these should be eliminated there we go to 3-1 bryggen well what if I need a version of this method here that does less than 10 as well well of course the unintelligent thing would be to copy and paste this and change this to a 10 and then now I'll get all numbers less than 10 and then hopefully you're starting to see if I want to do like 10 and then 13 and 20 and I mean this would this could get very pain painful very quickly but you notice the only thing I changed between these two copy and pastes are the 5 and the 10 okay oops alright I can eat so I'll also do what I want to do this I could say let's make you add another copy paste get all numbers greater than 13 okay so now I have to say well if the number is greater than 13 so yeah before the only difference was this 5 in this 10 so I could have passed like a you know int value and then replace this 10 with this but now on this on this third one here I've changed this whole expression this this less than went to a greater than and the ten went to a thirteen so I'm changing this code here alright I need to parameterize this code and this is a perfect place for delegates to do this so so that's what we're gonna do is say get all numbers less than five numbers and we need a delegate that can do this so it looks like the delegate needs to take it needs to take the number to do the comparison against and then it should return a boolean so so me delegate let's take the number int and then it needs to return a boolean a boolean expression okay and then get all numbers less than five we want to take what I call it me delegate so me delegate I'm going to call it the gauntlet all right basically if we send a number through the gauntlet and it comes out then we'll yield return it so let's just watch this this is the part where parameterizing here I'm gonna delete that I'm gonna say okay gauntlet the number run the number through the gauntlet and if the gauntlet returns true then yield that number okay so now I don't need all these copy and paste versions down here I can delete all them because I'm or I've allowed this to be flexible when I pass here so let's have a little fun with this first of all we need some some some methods so static ball less than five int n return and less than five all right I'm still gonna do a little copy and paste here and it's gonna be painful but I'll show you some better ways to get around this less than five like we did less than ten didn't we so now I change this to a 10 and then greater than 13 greater than 13 and so N greater than 13 okay so get all numbers less than 5 what we need to rename this we need to name it something more intuitive now because it's not less than 5 it's kind of anything so run numbers through gauntlet that's what we'll call it run the numbers through the gauntlet all right so I want these numbers in fact I'm gonna bring this out just to make a little bit more readable and just say enter a numbers put those right there and I'm gonna say run the numbers through the gauntlet and here's the numbers and the gauntlet we want to go through is less than 10 all right let's do that one first okay so it looks like we need some numbers that are bigger than 10 just to make this more interesting let's do 17 ok very good so let's let's here we go and if you good for you to type all this up and actually trace through it with the debugger kind of see what's going on but here you go less than 10 drumroll please here's all the numbers less than 10 days there they survived the gauntlet all right but watch I can parameterize this or I have to RAM rises so the second change that let's go less than 5 ok I'm pressing Q 2 3 1 alright in fact I'm gonna trace through this a little bit let's just less than 5 run F 10 if 10 runs through the gauntlet for each ok so here we come for each int number the first number dunno that is 2 so 2 we're gonna pass two to the gauntlet which is our delegate so here's two now remember our delegate is referencing less than 5 300 f11 so n comes down here or 2 - less than 5 is that true or false - less than 5 that's true so this this gauntlet invocation return true and suil the number okay and then we write line the number and then we go in let's do the next one what's the next number it's going to be 7 so I'll gauntlet on 7f 11 well 7 is not less than 5 that's false so then it'll skip that and go to the next one the next one is there's 3 is 3 put 3 into the gauntlet here three less than five yep that's true okay so yield that one and so on and so forth okay anyway so yeah we still had to do a little bit of copy and paste here but there's better ways around this I'll show in a future video let's go nice we just parameterize the condition here so I don't have to copy and paste this whole method over and over and over again
Info
Channel: Jamie King
Views: 98,846
Rating: 4.5543671 out of 5
Keywords: C#, Neumont University
Id: rxLNJ8jCN1c
Channel Id: undefined
Length: 9min 3sec (543 seconds)
Published: Tue Jun 12 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.