Delegates in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to lecture of Delegates in tisha from.net school now delegates in c-sharp is a very important concept regarding your interviews regarding your concept building regarding your usage so if you are programming in C sharp you will be using delegates extensively in your application also this is also very common question to understand that whether you are sound in your C shop concepts so most interviewers ask you that what do you mean my delegate so let's try to understand what are delegates in C shop now remember in this lecture I will merely explain you the concept of delegates let what does delegates do in in c-sharp and I will explain you the usage of delegates in my next lecture so it is very important for you to understand the concept first because this is a very important concept and you need to understand that why do we need delegates in c-sharp so let's look into the points which are mentioned in the slides so what are delegates a delegate is an object that knows how to call it method a delegate is a reference type instead of referring to an object a delegate refers to a method and this is called encapsulating the method so what do we mean by these points what I mean is that you can simply term delegate as a function pointer or a method pointer so first you need to understand the word delegate what does the word delegate means in the English terms say suppose we are watching TV and you need to change the channel and you are very far away from your remote so you will ask your brother or sister to change the channel for you right so you are delegating the work your work to change the channel to your brother or sister so this is what actually is meant by delegate same concept goes in c-sharp - so delegate is something which calls a method right instead of you a class calling a method or some other method calling a method a delegate does this calling for you or in other words delegate points to a method but instead of elaborating on these points let's first create an example and then let's create a delegate to method and then we will go through all these points one by one so let's move to Visual Studio so here I have a very simple class called s program and this class has a has two methods one is square method and money's main method so main method is the entry point of this class now what is this square method do this takes a integer right and then it returns a integer and what what does it return it returns a square of a number okay so this is a method we need to consume in our program class so now let's try to use this method without delegates okay so let's prompt the user first to enter a number so let me write please enter a number okay so the console will prompt the user and then I will capture that entry into a type of variable integer okay and how will I capture it by using console dot readline so whatever with the entry of the user that will be captured into the integer hi and you can see the red squiggly it says that you cannot convert type of string to integer that's because console dot readline returns a string so let me try to convert this into integer so let me have int dot Pass and then let's convert it into integer and store storing this variable into integer I okay so whatever I am entering it will be stored in integer I variable so I have got my entry recorded now I need to call the square method and I know I need to use direct the class name that's because it's a static method so program dot square and what does it Square takes it takes a integer of type X so I will pass my hi variable into it right and it will return a value so I will store that value into a another variable called as integer J okay and then I will write the output to the console so I will have console dot write line J and then I will have simple console Daughtery to stop the console so very simple program using this square function let's try to run this so it's asking me to enter a number let's enter 25 it gives me 625 or say let's do it again let's enter 10 it should give me 100 and it gives me so so it's my program is working fine okay so as I have told you a delegate is used to point to a function or method so let's use a delegate and then call this function for now we are calling this function directly with the help of this class name okay so there is high amount of coupling in here right I am you I need to use the class name or the classes object's name to call this method we can also call this with the help of a delegate and remember this lecture is just for you to understand the concept of delegates why do we need delegates that I will explain you in my next lecture so let's try to make a delegate and then call this function before that we should visit our slides for him for a point and you can see the last second point it says a delegate signature should always match to the method signature it is referring to so this is a very important point if you want to make a delegate then you need to match its signature to the method that it it will be pointing okay so you can see the point above that we can create a delegate with delegate keyword followed by a return type and the signature of methods that can be delegated to it so let's try to have these points into action so let me create a delegate with the help of delegate keyword okay so delegate and that's the return type of my delegate and let's name this delegate as transformer okay here it goes I have created a delegate now you can see delegate signature is very similar to mine delegates my method okay just it has a delegate keyword added before it so if you put a delicate keyword before him methods name it will become a delegate method it will not have the body like a method okay also you can see it's the signature of square function is matching with my delegate right it returns an integer my delegate to return say integer it takes an integer my delegates to ended it takes a integer so this delegate can point to this square function okay or it can also point to any method that returns an integer and takes an integer now delegate is a reference point a reference type right so what what can we do to access reference types we need to create a reference for it so first we need to create a reference for my delegate so let me comment out or remove this one and now I need to create a reference for my delegate so I will have transformer T okay so I have created the reference for might run delegate then I need to point this reference to my method so I need to invoke my square function so I will point it to my square function okay and then I need to invoke the delegate so I will have T dot invoke and then I will pass my integer variable into it okay the variable that I am capturing in here and it will return something so I will capture it in the integer variable J okay and then it will write the value of G on to the console and then it will have console dot read okay so I am not using the program class directly to invoke this square function I am using a delegate to do so so my delegate is invoking my square function now let's try to run this program so it Smith went ID number let's enter 20 that is all should be 400 and it gives me result s 400 so this is working as it was working before okay in sir the only difference is that we are using delegate to invoke this square function let's move back to our slides and you can see the third and fourth point have been validated in my demo so we can create a delegate with delegate given and written name and signature should be there with that delegate and that's the signature should match with that delegate and the method that it's pointing so same goes here I have created it with the help of delegate even and the signature is matching with my square function also after creating the instance we should point that instance to the method that which we want to call so this is what we are doing in here we are pointing this delegate instance to the method that we want to invoke in this case we are invoking square function we should then invoke the delegate white dot invoke or directly so we are then invoking this delegate in here with the help of T dot in book now what do I mean by invoking it directly is that I can directly point it to the square function in here ok and then instead of having this these two lines I can have directly pass the variable into the dedicate instance and store it in a variable called J okay let's try to run this please enter number say 10 it gives me hundred so instead of doing it step by step say creating the instance then pointing to the method and then invoking it we are doing directly in this two lines okay so this is the concept of delegates we are instead of directly calling this square function we are using a delegate to do so now there might be a big question coming to our mind that why do we have used delegate to invoke such a simple function right so this is this is what I'm trying to explain you in my next lecture so in very simple words delegates helps you to achieve decoupling or it helps us do to achieve the decoupling of your classes okay so if you are working in big projects like insurance projects or banking projects etcetera you will need to have understand you will need to use delegates more and more in your classes right it helps you to design flexible classes also delegates concept takes some time to get used to but once you get used to it it's a very powerful and very interesting concept let's move back to our slides all these points we have seen one by one so what are delegates delegates are simple function pointers and these delegates helps us to achieve the flexibility in our classes or decoupling the methods with glasses and where to use delegates that I will explain you in my next lecture so thank you so much guys for listening if you have any doubt please leave a comment under this video also please do like and share this video to your friends and please subscribe to my channel thank you so very much
Info
Channel: DotNetSkoool
Views: 17,180
Rating: undefined out of 5
Keywords: C# Training, DotNetSkoool, Delegates in C#, Delegates in .NET, Delegates
Id: iGIVlM0Ro3U
Channel Id: undefined
Length: 12min 38sec (758 seconds)
Published: Thu Jun 23 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.