Delegates in C# | Delegates Part 1 | C#.NET Tutorial | Mr. Bangar Raju

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to nourish technologies and balaraju and in this video I want to demonstrate out delegates so what are these delegates and why do we use this and delegates means it delegate means a standard definition about this particular delegate says it's a typesafe function pointer in the sense the delegate is going to hold the reference of a method and it is going to call the method for execution a delegate holds the reference of a method and then calls the method for execution it is going to call the method for execution basically so a delegate holds the reference of a method and then calls the method for execution so that's what a delegate is actually in c-sharp we can call the methods in two ways one by using the instance of the class if it is a non static method and by using the name of the class it is a static method so this is what you might be doing till now for calling the methods if the method is non static you will create the instance of the class and call the method if the method is static we are going to call it by using the class name and the second way how you can call the method is by using a delegate so the Delp of a delegate we can call the method so first let us understand how to call the methods by using a delegate in the latter videos we'll just understand what is the advantage of calling methods by using the delegate okay and in this class I want to define a set of methods so first public why a simple method called as Adams in TA entry the taking two parameters and inside of this I am just writing console dot write line a plus B and next one vomited public static strength say hello string STR or string in return hello class so these are the two methods what I'm just defining in my class now and if you want to call these methods now if you want to call these methods now how do you call the first approach of calling the method to call this method you require the instance of the class so let's create the instance of the class first so program class instance vampy is a calls to a new program program P is equals to new program ok creating the instance of the program class and after creating the instance of the program class we are going to call the matter P dot Adnan's 100 and some value next the second method is say hello and rhombus say hello is the static method so we are going to call it by using the name of the appropriate class so string STR is equals to program dot say hello calling the method and this method Oracle persisting is a parameter some name here and then console dot write line just yet console dot readline yeah without the result so this is all you're calling the methods till now you might be out calling the method still on like this but we can call these methods by using delegates so to call them matter by using a delegate okay let's see first to call it method by using a delegate we have three steps the first step is we need to define Italy it define a delegate so how to definitely get modifiers delegate avoid our type and then name and parameter list a parameter list so this is how you're going to define a delegate so if you watch this cleanly the statement looks very much similar to a method signature how do you define a method it looks exactly similar but an extra word what we are using is delegate other ways a method signature and a delegate will look exactly same only okay so now you need to define a delegate not just our look I want to call this method by using a delegate if you want to call this method by using a delegate so what you're going to do is see first step we are going to use a delegate key for you and reminding everything is same change the name of this particular method method name delegate name will not be same so give some name to the appropriate delegate so I want to name it as ADD delegate at delegate and then end it with a semicolon so if you noticed that this is our method signature and this is how our delegate looks like so both are looking exactly same but here we have delicate keyboard and here we're going to have a different name and the two important points to remember while defining a delegate for calling a method point number one the return type of the delegate should exactly be same as the return type of the method you see here the method type is void so delegate return type is also quite and the second pointer the parameters of the delegate should exactly be the same type of parameters to your method here the method is taking two integers and yet the delegate is also taking two integers see this names A and B are not important you can give any name you want that's not a problem at all you are using ax and here I'm using Y so the point is the intent should be same the integer what I'm using here and what I'm using should be same okay so these are the two important things what to remember first one is the return type of the delegate should exactly match with the written type of the method and the parameter types of the delegate should exactly match the parameter types of the method okay now let me understand how to define a delegate for the second one copy I want to definitely get okay public no need to use this static because the metal is static whenever you static on delegate delegate string and some name for this particular legate let us call this C really it and string said you name you give anything here that doesn't matter sting or not is only the point notice there sa on something and you see if the return type of the delegate is string because the metal certain type of string the parameter type of the delegate is string because the methods parameters also string so like this the delegate signature is going to match with the better signature signature in the sense return types as well as parameter types of your delegate are going to exactly match with the wreckin tides and parameter types of the method this should match so that is the reason why that is the reason why we say a delegate is a type save function pointer basically the delegate signature matches with the method signature everything is going to match the type save why because both are having exactly the same thing so this is the first thing what you're required to do come on let us define this delegates remember a delegate is a type a delegate is it type what is the type the class is a type it structure is a type an interface is a type all these are user defined types generally what are the class means what we answer a class is a user defined type a structure is a user defined type an interface is a different type same as that delegate is also a user defined type okay and generally if we understand the difference for each class and structure the main difference between two clans in structure is class is a reference type structure is a value type same way a delegate is also a reference type and very important point to remember is in C shop in C shop we generally define types under namespaces so every type is a defined under a namespace so if you are working with result studio every class you define will be under namespace same way a structure interface whatever you are doing the everything is going to be defined under a namespace why rights are going to be defined remains places in the namespace is a logical container of types and because the namespace is a logical container of tides we are going to define all the types inside the namespace so now if the question arises where I should define the delegate the answer is it should be defined under a namespace come on I'm going to put it you can't I define this inside a class yes you can you can define it inside a class also it becomes the nested type a type inside the type but generally not advised you better define it under namespace but it is still possible to define a delegate inside a class so this is the first step whatever step defining a delegate the first step is defining a delegate fine second step the second step is instantiating the delegate what is that instantiating the delegate actually in the process of instantiation we will create the instance of a delegate while delegate is referenced of the throat of telling you you're going to create the instance of the delegate and at the time so the delegate you're required to pass the metal name as a parameter to the delegate so what you're going to do see here here I called the method by using the instance of the class but now our plan is to call by using the delegate so what to do justly here we need to create a delegate now instance of the day layer which delegate at the rate a delegate 80 is equals to new add delegate cream instance and when you're creating the instance you notice the intelligence in the intelligence it is going to show you the information what information the delegate you need to pass a method as a parameter for it the target method should I have a parameter what is it for the delegate you are going to pass a method as a parameter a target parameter and the method should be void and the method should take two integers any method not like Adnan's here any method that is going to match with these parameters can be passed as a parameter for the delegate now and remember the target method should be non value returning and should take two integers as a parameter and our add nums is going to match with this so let us pass our method name here but an important thing to remember when I'm typing Adams is not coming in the West here intelligence see when you're writing the code intelligence should show you everything if intelligence is not showing you something is wrong there so why the intelligence is not showing my method name there the reason is your method is the non-static method and right now you are creating the instance of the delegate in the static block and the very important rule is a non-static member of a class can never be accessed from a static block directly you can access it only by using instance of the class so how to call it now TDOT Adams this is how you should write here directly we cannot write add nums so what happens now the address the address of the method is given to the delegate why delegate holds the reference or address of imitator so now this address goes to the delegate now same way let's do the second one also for the second thing see really good yes D is equals to new C really it okay now you just required to pass a target method and the target method should have a string input and a string output string input string output and remember our C hello method matches with the signature no and saying hello is a static method static - static directly we can access it generally because we have it in the class I can direct kill call the material how say hello because static members can be directly accessed under other static blocks within the class but outside the class we should prefix with the class name okay so I'm generally using the class name but not required program dot sale program dot say hello is how you are going to do it so this is the second step and this second step is what we call as instantiating the delegate the second step is instantiating the delegate first step is defining a delegate and the second step is instantiating the delegate so how do we instantiate instantiating is a process of creating the instance of the delegate and in the process you're required to pass the method name as a parameter to the delegate constructor you're going to pass the method name as a parameter to the delegate constructor so this is the process of you are going to perform the instantiation finally the third step what is the third step step trick what it is now call the delegate by passing required parameter values so that internally the method which is bound with the delegate gets executed so what you require to do you're now going to call the delegate by passing the required parameter values so that internally the method which is born with the delegate gets executed let us see how to do it now I want to call the method which method add nums but we are not going to call the method directly here what we are going to do is we are going to call the delegate how to call the delegate ad actually you can call of directly ad of C there when you just say ad off it is just asking you the parameter values and reckoned up everything it is telling you here so you can straightaway call this now we can straight away called us now like ad of some 100 comma 50 we can call it this and simha's is you come here how do you call string STR is equals to the SD of SD off past the parameter value what's it some name actually whenever you call this internally the method is going to execute or not the delegate delegate doesn't have any body delegate is a pointer to a method so whenever you are just trying to call the delegate it will execute the method the result comes to STR and prints you will get the same result what we got earlier don't expect any different output but earlier we called it straightaway and now we are calling it by using a delegate same answer same output and remember remember you can also try like this what's it dot invoke what's it under this delegate we have a method called invoke so it provides you a method called as invoke here and this is automatically generated for us is automatically generated and you see the error this invoke this invoke what you have here is taken two parameters int x and y it is evening 100 comma 50 means you can call it as a method now you can call it as a matter what imatinib invoke is the mentally invoke is the method name what you have here and here also as d dot will get the method invoke sting str bouncing there so like this also we can just call any of the two ways we can just call directly we can just call it a delegator or the second option is we can just use the method called as invoke that comes to you inside the delegate and call the things and once you pass it automatically the execution takes place we will see the same output now also so this is the process are you going to call a method by using the delegate okay thank you for watching the video for more videos please subscribe to our YouTube channel nourish IT [Music]
Info
Channel: Naresh i Technologies
Views: 141,731
Rating: undefined out of 5
Keywords: Delegates in C#.Net, Delegates, c#, c# (programming language), C#.NET Tutorial, Bangar Raju, Define a delegate, Instantiating the delegate, using a delegate, Delegates in C#, delegates in c# with example
Id: D8PyJyI4kb4
Channel Id: undefined
Length: 17min 56sec (1076 seconds)
Published: Wed Apr 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.