Part 36 - C# Tutorial - Delegates in c#.avi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to prism technologies i'm venket this is part 36 delegates in this session we will learn the basics of delegates in the previous sessions we have learned about structures classes and interfaces we know that structures are value types whereas classes and interfaces are reference types similarly delegates are also reference types now delegates is one of the complicated subjects in any of the programming languages but trust me if you get the basics rights delegates are extremely easy and you love to work with them in fact when I was learning delegates I was like what the hell why do we use this these are so complicated but then once I realized the flexibility they provide us with you know they are I love to work with them right now okay so let's get our you know hands around the basics in this session and in a later session we'll actually see how to apply this you know delegates to solve some real time business problems okay let's look at an example now what's a delegate a delegate is a function pointer actually a delegate is a type save function pointer so this is the actual definition of a delegated delegate is a type save function pointer meaning a delegate points to a function and then when you invoke this delegate the function will be invoked now you will be wondering why do you have to create a delegate make make it point to a function and then invoke that delegate why don't you call the function directly now the reason why we have to do it in that way is because of the flexibility that we will get with that approach okay that's what we'll examine in a later session but in this session we'll only concentrate on the basics of delegates so once we are very comfortable with the syntax of creating a delegate and then making it point to a function and then invoke the delegate and and we get all the you know basic strong about these delegates then we will see how to apply this delegate to a real-time project and solve some real business problems all right now since a delegate is a type save function pointer meaning it points to a function so we need a function so let's go ahead and create a simple function now I have a simple function public void let's say hello and this function takes in a message of string type let's say STR message and all this function does is output that message to the console so console dot readline STR message so it pinch that message to the console let's make it static so this static method and if you haven't you know watched my video series on methods and if you're new to methods I would strongly encourage you to watch the video tutorial on methods in this series okay now if you look at this method it has got the wide return type and it takes a string parameter okay now we know that a delegate is it is a function pointer so to actually understand the delegate you know think of it the syntax of a delegate is actually similar to a function okay now to create a structure we use the struct keyword created class we use the class keyword similarly if you want to create a delegate you use the delegate keyword okay so public delegate now to remember the syntax of the delegate think of it as if it's a method with a delegate keyword okay a method will have a return type name of the method and then any parameters okay similarly a delegate will also have a return type let's say why this return type and then the name of the delegate now this delegate is going to point to this function so let's give it a meaningful name you can give it any name but let me give it as hollow function delegate okay and then just like a method a delegate also takes in a parameter so I'm going to pass it a string parameter that's it so if you look at this delegate you know apart from this delegate keyword it's actually like a method signature that's got the access modifier a return type name of the delegate of function whatever and then the parameter but if you put the delegate keyword there it becomes a delegate meaning this delegate now can be used you know to point to a function which has a similar syntax I mean similar signature now this delegate can point to any function that has got wide return type and takes in a string parameter okay now how do we make this delegate point to a function okay to do that you have to create an instance of this delegate and this is where a delegate is similar to a class okay now if I have a customer class I can say customer c1 is equal to new customer c1 is the instance of the class similarly I can create an instance of this delegate so to create an instance of this delegate all you do is hello function delegate okay let's say delegate is equal to new hollow function delegate and to the constructor of this delegate you pass in the name of the function to which you want this delegate to point to okay in our example I want this function to point to this hello method so I'm going to pass in that function name and if you look at the intelligence here okay the parameter that you are going to pass in should be a method name because that's a delegate okay and that method should have a wide return type and it should take a string parameter so is our method confirming to that signature yes it has got wide reason type and it's taking in a string parameter so now I can pass this hello now to invoke that method all you do is invoke this delegate and if you look at this delegate it is expecting a string parameter to be passed in so when I try to call that delegate I have to pass in the string parameter so let's say hello from delegate so now what's going to happen this delegate call will actually internally behind the scenes it will automatically invoke this method and to this method it will pass this parameter and whatever this method has to do it will do its job in this case it will print that message onto the console so if I go ahead and run this now look at this hello from delegate we get that you know this is being printed by this method okay so it's it's very simple a delegate is a types a function pointer will come back to type safe part in just a bit but let's understand this a delegate is a function pointer why is it called a function pointer because to this delegate you're passing in the name of the function which you want to invoke and then when you invoke this delegate since this delegate is pointing to that function that function get called automatically that's why delegate is called as a function pointer because this delegate is pointing to this function so this delegate is a function pointer but what do we mean by types a function pointer okay actually in c-sharp delegates are types a function pointers what do we mean by that now let's say I have another method or let's take this method itself instead of the string return type I mean instead of the wide return type I am saying the return type is going to be string now look at this I have already caught a red squiggly indicating that you know hollow method has the wrong return type now this delegate if you look at the signature of this delegate it has a wide return type and it expects a string parameter but if you look at this method to which this delegate is pointing this method has got a different signature it has got it String return type okay so the point to keep in mind is the signature of the delegate should match the signature of the function to which it points to if the signature of the method does not match then we get a compiler error like this so that's why the delegate is called as a type safe function pointer so the signature of the delegate should match the signature of the function to which it points to that's the reason delegates are called types a function pointer look at this the moment I change that to void now the signature of this method matches the signature of this delegate hence the compiler warning goes away okay so let's go back to the slides now you might be wondering okay why all this complexity now all I want this I want to print this message hello from delegate by calling you know this hello method now why do I have to declare a delegate create an instance of that and into the instance I want to pass this method and then call the delegate okay so there are so many steps why do you want to do that you know instead of all this simply say hello and pass in the message hello from delegate okay which is actually very easy to do and you get the expected output but then here we are trying to learn the delegates okay so actually delegates give us a lot of flexibility in real-time projects okay which we will be exploring in a later session so don't worry about that piece now okay so that's why we are trying to understand the basics of delegates here okay so what does a delegate a delegate is a typesafe function pointer that is it holds a reference or a pointer to a function and the signature of the delegate must match the signature of the function the delegates points to otherwise you get a compiler error and this is the reason why delegates are called as types a function pointer and if you look at it a delegate is very much similar to a cup class you can create an instance of set and when you do so to the constructor of the delegate you in the name of the function that you want your delegate to point to and a tip that I followed to remember you know the syntax of the delegate it's very much similar to the syntax of the method except that it has delegate keywords within that just like a method it has got a written type name of the method or delegate and then the parameters with a delegate keyword and this that simple example that we have seen okay if you look at this delegate it has got signature very much similar to a method with the delegate keyword and here we are creating an instance of this delegate and to the constructor of the delegate we pass in the name of the function and this functions signature must confirm to the signature of the delegate otherwise we get a compiler error that's why we call a delegate is a type save function pointer and then to invoke the delegate we just invoke the delegate and pass in the string message which will behind-the-scenes automatically invoke the function to which this delegate is pointing to all right on this slide you can find some resources for asp.net and C sharp interview questions that's it for today thank you for listening have a great day
Info
Channel: kudvenkat
Views: 576,543
Rating: 4.889688 out of 5
Keywords: delegates, in, c#, c# delegates basics, c# delegates explained, c# function pointer delegate, delegates c# programming guide, public delegate void c#
Id: D2h46fvQX04
Channel Id: undefined
Length: 11min 53sec (713 seconds)
Published: Tue Jun 19 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.