C# Delegates explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
right good afternoon everyone so in this presentation we will talk about what is a delegate right so if you see one of the standard answers in the market about delegate is that delegate is nothing but it is a pointer to a function okay when I look at this definition you know it looks very very monotonous to me okay and also it looks very stupid I'll tell you why for example let me go to small delegate here and then we can you know just discuss out you know what is the need of delegate okay so if you say delegate is a pointer to a function that means you are seeing something like this for example let us say I have a method here static void some method let us say okay and in this some method let us let me go and create a simple console dot write line saying method called okay now if you say delegate is appointed to a function you know that means if you see that if you see the code of delegate it looks like this you know so basically you go and you create a delegate so like this saying some method pointer right so you can see that I have created a delegate called as some method pointer and later what I can do is I can take this point or I can create object of this pointer and I can point it to that some method right I can point it to that some method and I can just go ahead and invoke that object so you can see now you can see that I have created a delegate and this delegate some method pointer is pointing towards some method and I am saying invoke alright so if I do a control f5 over here you can see that my method has been invoked but now does it make sense does it make sense you know I mean I can call this some method myself I can just say some method that's it right why do I have to go indirectly and call this some method are we getting guys in other words can't I do something like this I can just go ahead and say you're some method right so the next question to think about is that basically what is the meaning or what is the what is the benefit of calling a method directly and what is a what is the benefit of calling a method indirectly why a delegate pointer right so this is the understanding of delegate technically but from the real point use you know we still know we are still not getting the answer that why do we need delegates okay a method which can be called directly what is the point of calling that method indirectly so in order to get the real essence of delegate or in order to really understand that why we need delegate let us try to go into basics you know let us just go back to our school days and let us try to understand that what is the English meaning of delegate what does this this dictionary meaning of delegate means okay if you go and hunt around right the dictionary meaning of delegate you will come to know that delegate is a kind of a representative you must have heard the sentence now and then saying that okay the u.s. delegate went to this country and did this the Indian delegate visited to this region and did this right so basically if you look at the real world or if you look at the English meaning of delegate actually this delegate word is used you know whenever you know you are talking about a representative okay and the main goal of this representative is to communicate between two parties so when I say that Indian representative has been sent to Nepal that means you know this representative is going to communicate between Nepal and India right so that is the meaning of delegate you know when it comes to real world and let me tell you you know that is what exactly is the meaning of delegates even in sheesha at the end of right so delegate here in c-sharp is meant to do communication between two things okay so let me explain you what does it mean so now assume for example so let me go and delete all these codes over here what I've written now assume that you have a class you know so let us say you have a class here called as class my class and this my class has some long-running method you know so I'm going to go and create something saying long-running okay so there is a long-running method over here and inside this long-running method you know let us go and create a for loop kind of a huge for loop which runs something like ten thousand times right and it loops through this and does something okay now this my class I am calling it from this program class here or from from a static void mean I'm trying to invoke my class over here I'm going to say obj dot long-running right so now here we have two parties actually so one party is this class program my static void main and the other party whom I am calling is my class okay now let us say this first party or the first entity over here my the program wants to get informed that which number is at this moment running in the for loop so he wants to get updates alive from this long-running method okay so what do we need what we can do over here is we can go and create a delegate right so I can go and say that and this my class I can go and create a delegate here okay saying call back okay and this my class will say that okay anybody can pass me the delegate function here and I will ensure that I will make a callback and pass him the current running number right so you can see now this my class is saying okay send me or delegate via this and I will ensure you that I will send all the messages wire the delegate so now what this program over here can do is he can go and pass him a method name right so in other words let us say that I have some method here called has a callback right and console dot write line so I can go and pass this callback method right to this to this my class over here so you can see now I am passing him this callback method and this this callback method is now becoming a delegate right so it is a delegate to this my class so now what is my class does is it actually goes and calls back why this delegate and informs the program right that which is a current running number so if I do a control f5 so if I do this so there you can see now numbers are getting displayed so in other words you know what is happening is that basically this my class is communicating the data via the delegate so the real use of delegate write in C sharp is callbacks callbacks and callbacks right so the way we say in our real world you know that delegate is nothing but it is kind of a representative you know who helps us to communicate in the same way here in c-sharp you know delegate is nothing but a representative you know which will help us to call back and do the data communication so I hope that you know this presentation was useful in this presentation I was trying to bring out the real essence of delegates and trying to make you understand that what exactly is a need of Delegates in C shop so next time you know when you talk about delegates don't just say that delegate is a pointer to a function delegate is that delegate is that the main use of delegate is communication between two parties the main use of delegates is callbacks callbacks and callbacks thank you
Info
Channel: .NET Interview Preparation videos
Views: 429,977
Rating: 4.653028 out of 5
Keywords: c#, delegates, Delegate, csharp delegates basics, explain delegates, c# function pointer delegate, delegates c# programming guide, Delegates in C#.Net, Define a delegate, Instantiating the delegate, elegates in c# with examples, using delegates in c#, what is delegate in c#, c# tutorial, c# video, csharp interview questions and answers, csharp tutorial for beginners, delegates], c# delegate example, delegates in c# example
Id: ifbYA8hyvjc
Channel Id: undefined
Length: 8min 46sec (526 seconds)
Published: Tue May 12 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.