Part 38 - C# Tutorial - Delegates usage in c# continued.avi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to prism technologies I am venket this is part 38 Delegates usage in c-sharp part 2 in the previous session we have seen a very simple program a very simple employee class where we have created a promote employee method and if you look at this promote employee method you know the problem with this method is that we have hard-coded the logic of promoting employees okay the logic based on which promote we promote employees is basically hard-coded and we know that these delegates are extensively used by framework developers now if you are a framework developer and you're developing class and methods for a framework then you want your classes and methods to be reusable and you don't want to be hard coding logic like this so if you hard code logic like this you are you are preventing your class from being reused so now let's go ahead and see how to make this method reusable with the help of a delegate okay in part 35 we have seen how to use you know the basic syntax of a delegate and to create a delegate we use the delegate keyword so delegate and we know that a delegate syntax looks much like that of a method okay a delegate will have a return type it will have a name and then parameters are obviously optional okay so now what we are going to do is all we have to do in this method is we want to replace this logic based on I mean instead of hard-coding this we want to be in a position to be able to pass in maybe a method as a parameter here okay we will understand that in a bit so let's say so I want if you look at this condition what is this condition doing okay this condition will either return a true or a false okay employee dot experience greater than or equal to 5 you know if that is true then the employee is promoted otherwise it doesn't get into the if block so this expression basically returns a boolean true or false so our delegate also should return a boolean true or false and then let's call this is promotable that's the name of the delegate and if you look at this expression it's actually making use of an employee object so for our delegate we need to pass in an employee object so let's say empl okay that's it so we have created our simple delegate so what will this delegate it's going to return a true or false for the past and employee if the employee is eligible for promotion then it returns true otherwise it it should return false ok so now what we will do we need to replace this expression with that delegate and the way we have I mean to do it it's like this so 2 into this method I'm going to pass in another parameter and that parameter is going to be my delegate so I'm passing in a delegate as a parameter to this function okay so I am passing in this delegate so let's call this is le cheaper to promote just a meaningful name ok now look at this we we are passing in a delegate as a parameter to this remote employees method now we know that a delegate is a function pointer in the sense this delegate will be pointing to a function ok so at runtime what happens instead of this delegate you know when you invoke this delegate it is going to invoke that method to which this delegate is pointing ok so effectively this this function is taking in another function as a parameter okay so so anytime if you want to pass in a function as a parameter then think of delegates all right so now we passed and this delegate as a parameter so how are we going to replace this logic here is get rid of that logic there and then simply say ok if the employee is eligible to promote and obviously this will take an employee object so let's go ahead and pass that so now there is no logic whatsoever here it's just a delegate you know the frame you know we know that this delegate is going to return true or false if it returns true then that employee gets promoted otherwise he will not so where is that logic of how to promote an employee's based on that will be decided by the end user who is going to make use of your class now if you look at your class it's very clean it doesn't have any hard-coded logic within that so let's go ahead and see how to use this class okay so the person who is going to use your class now to you know call the promote employees method when he calls the promote employees method initially it was just Employee List but now it's going to be a delegate as well so you have to pass in a delegate as well now we know that a delegate is like a class when you create an instance of the delegate the constructor will actually take in the name of the function and we know that that function signature has to match the signature the delegate so let's go ahead and see that so now this employee promote employee method is also taking another parameter you know is promotable which is nothing but a delegate okay so if you look at this it's the same example that we have seen in the previous session we have an employee list and to that employee list we have four employees okay now let's see how to invoke the method first we need to create the delegate what is the delegate is promotable so I'm creating an instance of this delegate is promotable so from more trouble maybe just give it a meaningful name is promotable that's the name of the delegate so we are creating an instance and we know that to the constructor of this delegate we have to pass in a function which has got a boolean that written type and which takes an employee object as a parameter so we first need to create that function and it is that function where we define our logic on how we want to fire our employees I mean promote our employees so let's go ahead and let's say public boolean let's say promote and this method should take an employee object and let's say EMP and now here you will provide your logic if let's say EMP dot may be experience is greater than or equal to 5 years then return true else return false okay so if you look at this method it returns a boolean and it takes an an employee object and we have to pass that method to this constructor of the delegate so we are going to pass in that promote method so we have a delegate created now and let's make mark the static so that we don't need the instance the class to invoke that all right so we have the delegate now what we do is we pass in that delegate as a parameter this function that's it so if you look at this function now I mean if you look at this code we haven't hard-coded any logic in our framework method you know our employee class is going to go into some kind of a framework and if we don't want any kind of hard-coded logic there so we move that out you know with the help of this delegate okay so this delegate at runtime is promotable delegate is actually pointing to this particular function and this function will return a true or false if it returns true then we know that that employee is eligible for promotion and he will be promoted okay this way actually you can decouple the logic from your framework class and method you know which makes your class and methods a little more flexible so if we go ahead and run this now you know as far as the output is concerned it doesn't change in any way except that we have three structured our program using delegates which makes it more flexible and you might be wondering alright you know just to do this simple thing you know initially it was console.writeline you know you have your condition hard-coded here and it was very easy but now you know just because you you are using a delegate okay first you have to create that delegate and then you have to create this method which you know this method signature should match the signature of this is promotable delegate and then you need to create an instance of this delegate and and to that instance I mean to the constructor of the delegate you have to pass in the name of the function so a lot of steps to do these simple things actually do we have to do all this not necessarily you know if you are aware of lambda expressions these del lambda expressions are actually based on Delegates so instead of creating this public I mean this method and then creating your delegate and then to the delegate passing in this method what you can do is actually you can get rid of this function altogether you don't require that anymore and you don't even require this delegate you get rid of that as well now in inside this method you can actually pass in a lambda expression okay so instead of this delegate what you can do is now whatever goes in here it should it will operate on an employee object and it is going to return a true or false okay so what we can actually do is so you can say now just to give it a meaningful name employee such that EMP dot let's say experience greater than or equal to five okay so you can use this in-line lambda expression here instead of creating a delegate a function and then making the delegate point to that solution okay so you can do that this way so if I go ahead and run this now you know it's just going to be very simple I mean the same output except that we are now using lambda expressions instead of creating an instance it delegates the same you know behind the scenes what happens is the runtime actually creates a delegate creates a function and then passes it to this particular framework method which does its job okay so I hope you have understood that you know the the fact that using delegates will make your programs more reusable all right that's it in this session and on this slide you can actually see some s we don't let C sharp interview questions thank you for listening have a great day
Info
Channel: kudvenkat
Views: 375,443
Rating: 4.9263458 out of 5
Keywords: Delegates, usage, in, c#, c# delegate tutorial, c# delegates for beginners, c# - delegates real time example, real world example of delegates in c#, c# delegates practical uses, practical example of delegate in c#, use of delegates in c#, simple delegate example c#, c# delegates tutorial for beginners
Id: s0tkKZoMN1Y
Channel Id: undefined
Length: 11min 33sec (693 seconds)
Published: Wed Jun 20 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.