Understanding delegates in C# | C# Events & Delegates | Advance Concepts of C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to Pro Academy classes in this lecture you will learn about the basic concepts related to delegates in C sharp delegate is a complicated Topic in any of the programming languages but trust me if you understand the basics right delegates are extremely easy and you will love to work with them in this lecture you will learn what is a delegate how to create a delegate and how to use a delegate so what is a delegate a delegate in C sharp is a typesafe function pointer now in this definition there are two points to note first of all a delegate is a function pointer and second it is a typesafe function pointer let's try to understand these two points with an example here we are creating a method and we are calling this method addition now for this method the return type is integer and this method is also going to take two parameters of type integer and finally this method is going to return the sum of those two integers now when we create a method like this in C sharp and when we run the C sharp program in the systems memory this method will be stored somewhere now we have learned that in memory the memory can be classified into two types the stack memory and the Heap memory in the stack memory all the value types are stored and in the Heap memory all the reference types are stored so a method in C sharp is of reference type that means this addition method this definition will be stored in the Heap memory so let's say this method definition is stored at this memory address in the Heap memory now for this method we are also providing a name we are calling it addition so this addition will act like an identifier and this identifier will be stored in this stack memory and this identifier is not going to store the method definition itself it is going to store the address of the Heap memory where that method is actually stored so in the Heap memory this method definition is stored at this memory address so this addition identifier here it is storing that memory address now let's go ahead and let's create a delegate so to create a delegate we use the delegate keyword okay so here I'm creating a delegate and I'm calling it sum now don't worry about the syntax here we will understand this syntax practically but here just understand that we are creating a delegate called sum using this delegate keyword now we need to instantiate that delegate we need to instantiate this sum Delegate for that just like we instantiate a class in the same way using the new keyword we can instantiate a delegate so the name of the delegate is sum we are providing a reference variable name and then we are using the new keyword to create that delegate and then we are calling the Constructor of the delegate okay and to this we are passing this addition method okay we are passing this addition as the parameter to this sum so this addition is basically this addition method so now what will happen is again in this stack memory an identifier with this name sum will be created and this identifier will also point to this method definition okay so what this addition is storing it is storing a memory address as you can see here so when we are passing that value that memory address to this sum Constructor basically when we are instantiating this some delegate this sum variable it will be assigned with that memory address the memory address which this addition identifier is storing so now this sum is also storing that memory address and what do we have at that memory address we have the definition of that addition method at that memory address so now this sum is also pointing to that definition to that method definition and that's why we say that a delegate is a function pointer because here this sum delegate the instance of this sum delegate it is pointing to a function which is stored in the heat memory and we also say that a delegate is a type safe function pointer why is that that's because a delegate can point to only those functions which matches the signature of the delegate for example when we are creating this delegate here a delegate also has a signature just like a method here we have an access modifier then we are using this delegate keyword and then we are specifying the return type here the return type is end we are specifying a name for the delegate and this delegate is also taking two parameters of type integer type so if you notice this signature here matches the signature of this addition method it also has a return type of integer and it is also taking two integer parameters so this signature here matches the signature of this addition method and that's why we are able to point to this addition method using this sum delegate and that's why we say that a delegate is a typesafe function pointer because a delegate can point to only those functions which matches the signature of the delegate all right now keep in mind that just like classes and interfaces delegates are also reference type now let's go ahead and let's understand delegate with a practical example here I have created a new console application project and I am calling it events and delegates in this project we have this program.cs file and in this file we have this main method now apart from this main method let's also go ahead and let's create a new method and let's call it addition so for that let's use this public access modifier and this method is going to return an integer value let's call it addition and this method is going to take two integer parameters let's call it int a and int B okay and from within this function let's simply return the sum of a and b all right so here we have created this addition method now let's go ahead and let's create a delegate and to create a delegate let's go out of this program class so keep in mind that you cannot create a delegate inside a class a delegate should always be created outside of a class so here again I will use this public access modifier then to create a delegate we need to use the delegate keyword then we need to provide the signature for the delegate again for the signature of this delegate let's say it is going to return an integer value let's call it sum and this delegate is going to take two integer parameters let's call it into X and end y just like we saw in our slides so here we have created a delegate now let's go ahead and let's instantiate this delegate for that let's go to this main method inside that let's go ahead and let's instantiate this sum delegate so for that let's say sum let's provide a variable name again I will simply call it sum with s in lowercase we need to use the new keyword and then let's say sum so basically we are calling the Constructor of this some delegate now here you see we can see this red is quickly and it says sum does not contain a Constructor that takes zero arguments so basically here we need to pass the method name to which we want this sum delegate to 0.2 here let's say we want this sum delegate to point to this addition method so I'll copy this method name and I will pass it here now here I cannot simply use this addition because it is not a static method it is an instance method so for that first we need to create an instance of this program class so let's say program P equals new program okay so here I'm creating an instance of program class and here let's simply say P dot addition now when you are passing a method name to the delegate you need not to use this parenthesis like this okay when we use the parenthesis on the method name it simply executes that method here we don't want to execute this method we want to pass this method as the parameter to the sum delegate all right so now this sum is pointing to this addition method the definition of this addition method in the memory and since this sum is storing the definition of this method basically a reference to this method definition when we use parenthesis on this sum like this it is going to execute that method now this method is going to take two parameters so let's pass the value for those parameters maybe 20 and 30 and let's create a variable we know that this method is going to return an integer value so let's create an integer variable let's call it result and to this result we want to assign the value which this method is going to return keep in mind that here this sum is pointing to this addition method so when we are using parenthesis on this sum it is basically going to execute this addition method okay this addition method is returning an integer value and we are assigning it to this result variable now let's go ahead and let's log this result variable in the console and here let's say sum is equal to and then let's print the value of this result variable let's go ahead and let's run this program for that I will press Ctrl F5 on my keyboard so the project is building and here you can see sum is 50. all right so here we created this delegate we called it sum using this delegate we created a variable and that variable is pointing to this addition method so using the delegate we can point to a function and that's why a delegate is called as function pointer and using a delegate we can only point to those functions which matches the signature of the delegate here the signature of this sum delegate matches the signature of this Edition delegate because both of them are having a return type of integer and both of them is taking integer values as the parameter so here the signature of the delegate matches the signature of this addition method that's why using this delegate we were able to point to this addition method now let's go ahead and let's create another method so let's specify the access modifier as public this method is not going to return any value so the return type is void let's call it maybe print okay and this method is going to take an string parameter let's call it maybe message and what this method is going to do is it is simply going to log this message in the console all right now what do you think can we use this sum delegate to create a variable which will point to this print method let's try that so here let's again go ahead and let's try to create an instance of this sum Delegate for that let's say sum and I will simply call it print equals new sum and to this let me pass this print method and again since it is not a static method we need to use it on the instance so here let's say p dot print all right now here you will see we have this red squiggly okay and it says no overload of print matches delegate sum that's because the signature of this print method does not match the signature of this sum method that's why we cannot pass this print method as the parameter to this sum Constructor all right so let's go ahead and let's create one more delegate again first we need to specify the access modifier then we need to use the delegate keyword then we need to specify the return type here the return type for this delegate is going to be void and then let's provide a name let's call this delegate Maybe print Del and this delegate is going to take a parameter of type string I will simply call it message okay so here we have created delegate now let's go ahead and let's instantiate this delegate so I will remove this line and let's instantiate this print delegate so printl maybe I'll call it print equals new print Del okay so here we are going to create an instance of this printl delegate and to this Constructor we need to pass a method to which we want this print variable to point to for that I am going to pass this method and since this method is not a static method first we need to use the instance of this program class which is p Dot and here you can see we have this print method all right now we can invoke this print method using this print delegate so for that we can simply say print and then we can use a set of parentheses like this or what we can also do is on this print we can call this invoke method so for the Sprint method if you notice it is taking one string parameter let's pass the value for that parameter and here let's simply say hello from C sharp all right with this let's save the changes and let's run this program by pressing Ctrl F5 and here you can see hello from C sharp and then from the first delegate we are getting this result sum is 50. all right so in this lecture we learned what is a delegate a delegate is a typeset function pointer we also learned how to create a delegate so here we are creating two delegates and we also learned how to use a delegate so to use the delegate first we need to instantiate that delegate and then we can call invoke method or we can simply use the parenthesis on the instance in order to call a method to which that delegate is pointing to all right now you might ask why do we need a delegate to call a method we can call this print method or this addition method directly inside this main method right for example we can simply say P dot addition like this or P dot print like this right then why do we need a delegate to call these methods well delegates are not basically used to call a method all right the main use of delegate is to pass a method as a parameter as a callback function to another method we don't use delegates to Simply call a method okay we use a delegate for passing a method as a parameter to another method for example let's say we want to pass this addition method as a parameter to this print method how can we do that in C sharp we cannot pass a method directly as a parameter to another method but if we want to do so in that case we need to use delegates and in the next lecture I will show you how we can do that this is all from this lecture thank you for listening and have a great day
Info
Channel: procademy
Views: 4,073
Rating: undefined out of 5
Keywords: delegates, C#, C# tutorial, what is delegate, creating a delegate
Id: jfe77Nb-oog
Channel Id: undefined
Length: 16min 4sec (964 seconds)
Published: Tue Nov 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.