Learn and Understand C# Delegates by coding | Sandor Maraczy | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Learn and Understand C# Delegates by coding

teacher avatar Sandor Maraczy, Microsoft Certified Software Developer

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

9 Lessons (55m)
    • 1. What is this course?

    • 2. What is a delegate?

    • 3. Create a custom delegate in code

    • 4. Code duplication is growing - an example without using delegates

    • 5. Refactor code to use delegate

    • 6. Adding multiple delegates to the invocation list

    • 7. Delegate inference

    • 8. Action and Action of T

    • 9. Func of T, TResult

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

Understand the purpose of using delegates, and see how powerful they can be, through examples.

Delegates are very useful in C#, but the concept behind delegates might be hard to fully understand, therefore can be confusing for a lot of C# developers.

I will be your guide through your delegate learning path, and make it simple for you to understand through my logically built examples.

If you like my course, please feel free to leave a comment, and tell me what you would like to learn next!

Meet Your Teacher

Teacher Profile Image

Sandor Maraczy

Microsoft Certified Software Developer


Sandor Maraczy is a Microsoft Certified Software Developer. He became passionate about programming and automation in the beginning of college, where he studied Business Information Technology.

He first started to deal with programming in Java and C#, and he always felt, that he is interested in coding in more depth, so beside college he also improved himself at home. He found really good resources online, by a guy called John Sonmez, who inspired him. He learned to code step by step by actually doing it, so he could soon break into the field as a software developer.

He also helped a lot to hiss classmates at college, where he got a lot of compliments for the way he explains basic programming concepts.

Today he is also teaching online, because he believes, that throu... See full profile

Related Skills

Technology Web Development

Class Ratings

Expectations Met?
  • Exceeded!
  • Yes
  • Somewhat
  • Not really
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. What is this course?: Hello there. Commuted the learn and understand C sharp delegates by coding course. My name is Chandler Morality. Now you show you what you learn in my course. Why you should learn C sharp delegates. Delegate is an extremely important part off the darknet framework and they're commonly used these after a development, it can be sometimes the easiest solution to your problems. You only need to recognize the scenario when to use it by learning delegates, you will be able to write more powerful code which will allow better code reuse your software. He will use them for events which I will only clarify in the future. Sections off discourse and you will use them also indirectly Van, for example, creating glink you queries or operating with framework which are based on forming lambda expressions. At the end of the scores, you will know what a delegate is on those on the concept behind it. You will be able to create and use delegates and also understand the purpose when delegates can be useful, which I will demonstrate through my logically bigger and useful examples. You will see how delegates can help you avoid called duplication in your software and also how not to violate the open gross principle. And at the end, I will clarify what an invocation a list is. And Tom. Multiple delegates can be added to it. So let's jump right into it and code with me to become a delegated master. 2. What is a delegate?: delegates in C Sharp is a very hard concept to understand and can be confusing for a lot of C sharp developers. That's the reason why I chose this topic to give a briefing connection off that's jump right into the topic. I assume you're your family are variable initialization in C sharp. But let's step back to you just for a few seconds. In the 1st 2 examples, we have to value to act variables X, and it's finished axes off type int and we are assigning six to eat as a value. It's finished is off, Taibu and we are simply assigning force to it. Insert case. We have a reference dive variable called O off type object, and in this case we are creating a new instance off the object type, and we can reference to that object through our over able on the heap. But what happens if you don't want to hold any values or referenced objects, but we want to reference to methods, and that's going to take us to the concept of Terra gates because what is a delegate? Basically, a delegate is a type in C sharp, and I would demonstrate this with a program called I Spy. This is an easy way, how you were able to disassemble the code back to intermediate language or back to C Sharp . And after building up the simple project, I opened up the binaries in I Spy and I got the following output, as you can see on the slide. So in the first case in our C sharp code, we can see our delegate declaration is just one line. But in the second case, if I go ahead and speech this too ill to intermediate language, we can see that a class is generated for us behind the scenes. And this class extends more dycus delegate, and that is the base class here. Let's see what, as can be a delegate delegate, is actually a function pointer, and this means that we are able to reference to methods through our delegate. We are able to reference to warm matador home function, and we are also able to referenced the multiple functions or function group as well. We can also think of a delegate as a pipeline, very can just simply dump our data and we can get it from point A to point B, and it's very important to mention that these points can be a totally different points off our application. Now let's see how custom delegates work in this simple example. We have two static methods. They all return void and they all take string parameter here. In the first case, we have the lock to console method, which simply writes the message out of the console in the locked, locked off fire method were logging the string to your file. We can involved these methods one after the other without using delegates very simply with same or even different input. But we can invoke one or both of them through a delegate, and for that we need the following delegate declaration. So this is a delegate declaration, and as you can see, it looks like a method declaration. It's very simple. We need to use a delegate keyword here. We have avoid the return time because we would like to use the lock to console or the lock to fire methods years and they orator roid. Then comes the name off the delegate, which will be my longer delegate in this case, and then we need to write the parameters D Assigned methods values off course. We need an instance creation as well, which means that we need to create a new instance off this delegate type. We are saying, my longer delegate, my delegate equals new, my longer delegate and in the parentheses we need to write a method, and that method will be the method We are referencing, too. So where my delegate variable and we can invoke its very easily as mentioned through our my delegate variable bypassing Hello Vered The string input. We are able to call the lock to console method here and this simple program. We write a single line in the console which says, Hello word and that's it. It's so easy to define a custom delegate. Now let's see how it works in code. 3. Create a custom delegate in code: Now let's see how to define accustomed delegating code. In this simple example, we have two static methods, some on the subject methods here. They both take two ins as a parameter, and they return and into the result of the calculation. Of course, he won't surprised if we are calling them directly with, for example, five and five parameters. We will get 10 by calling some, and we'll get zero by Corning, subject as the result. So let's trying to sound just to make this shore. So I mean, first, call some weed mentioned five and five parameters and in the Saab, tricked into five five parameters here. And since these methods return in integer beneath to write console right line because it would like to see the output result on the console. So it's right because all of that right land here very quick, and now we fire on this, we can see that we get 10 and zero as theirs at just be expected. But what happens if I don't want to call these methods directly? But I want to use a delegate for the method cause this is also very easy to do, and it happened the same way we have seen earlier on the slide. First we need to delegate declaration forties. So I will make this declaration first here in this file and also here in the name space, so that other classes can use it as well. Maybe later on. And I really make it here and writes public. We need to use the that a gate keyword and scenes. These methods return, and we need to write an end as well as the return type. Then we need to write delegates name which will be numbers process delegate in this case and then the parameters Here the reference methods view use. So the reference methods are this some and the south directly want to use for delegation and they take two integers as a parameter. So we need to write. I didn't Thanks in tow. I hear as well. So we have our delegate declaration now, But in order to use this, we also need to instance she ate this delegate type just like it's so on the slide. Let me just first get rid of this direct invocation here. So I would delete this and I rinsed, initiate new object off this type your instance Here I have you called the Variable del and let's make this sequel to new numbers process, delegate. And now you can see that this actually takes a method as the target. And this method needs to have to enter parameters and need to return and as the results. Since we have two of these methods, we can use either some or the suffering. But let's try that for us with some. So I write some here and you can see that it complies perfectly to invoke it. We need to. He was our delivery will, and we go ahead and involved. It's variable. Just life. He would invoke a method and passing event five, just like we had earlier. Of course, we need to write it out to the consul as well, just like we wrote it earlier. And now a fire on this. We'll get 10 as well, just like we've gotten before. Because actually, what we need here is that we created in you delegate here, and the instance created this delegate and through Dezell Variable, we were able to point to this some message here. So by invoking they'll and passing in five and five parameters pointing to this method, we could simply and walk it. And we are also able to change this, to subtract if you want to by just quickly modifying some to subtract here. And now if I run this well, you and gets you know, as a result, I noticed that when I was changing some to subtract here, I wasn't touching this invocation here. So I was able to do this without modifying the invocation. Vit the five and five parameters. And this is not really useful yet because you may ask the question that why isn't it not enough for us to call these methods here directly? Why do we need to create delegate here and great new instance and invoke it through this variable? And that's what we are going to cover next. 4. Code duplication is growing - an example without using delegates: And now let's see how we can make real use off the delegate. Let's imagine that we have a new requirements instead of just in walking these methods through a delegate and praying the result of the council. We now want to be able to look the numbers, the name off, the method we are using for our calculation and the calculation results to a file. And we also want this to work for our future functions. This means if I want to add the function that multiplies or divides my numbers, I also need to be able to do this very simply. So let's see how it works. First, I will call this without using the delegate. Then I very factory to use the delegate to see how flexible our code can be. So let's start this by creating a class for our function. Because these methods are calculation related methods. We need to encapsulate dysfunctionality here. So let's create my calculator class. I will add new glass. Let's call this my calculator here and in this class. I would like to baste my calculation related methods from the main program, so I really quickly jump here and based these methods. And I will also get rid of the study keyword here. This means that in order to he was these methods. Now we need to create a new object off this type and in the main program. We also have our delegate here that we decorated here in this file. First, we will also create a new five for it. So less crate, new class gods numbers Croesus delegates here, and the class is generated for us automatically here. But we need to get rid of this because we don't need a class here. No aerial base, this Doug a declaration to this file. So let's see how it works. So as mentioned, we need to log the numbers, the method name and the reason toe file. So I will change this to not to return X plus y, but I will make new variable off type. And my real modified its that I was some These two integers and storing the results variable. Now I need to declare the method name. I write shrink with the name and it's booby, some very simply and now a riding out with text. So that's right. String output text equals formatted string here. So I write String that format. That's right. The numbers uh zero one de method name is to and do Result is three Now I need to ride the variables here So I write X and y seems these are the numbers I ride the method name which may be the method name and then I ride the result Now in order to look this to five you're right file here We need to use thesis Tamayo name space here and we're used the upend or text methods here any the past First on the bus will be Oh my c drive in the results for Inter So I will quickly coping this from here and now we write the path here when I just copied and let's create a new five for it The name off reason that txt And you also need to write the contents here which really are output text and now is the right step I simply return the result here in order to use this idea quickly involved this method Here I am right My calculator car geek was new My calculator since these methods are Inna encapsulated in this type here. We need to create a new instance to use them. I all right, God, that some for me The same bar may 3rd views earlier. And since these methods returns and are you right, guns? All right, line again here. Now, if I run this with you and get 10 as we've gotten before. But we also have a text file here. If I open the tax fight, I can see my output result. The number is the method name and the result as well. What happens if I want to use this to the subject method as well? Not just not only for two, some method. In order to do this, we need to get back to my calculator class very quick. And we need to duplicate this functionality to have a subject minces Since these files upend all text method simply writes the apple strings without white spaces or without any new line. Ivy quickly modified this here to string that conquered die right out with text and make a new line after that. So now let's go ahead and create our subject. Metal Justice mentioned diving copy paste this and let's call this subtract here and here as well. And there is one more modification. We need to take that instead off summing Exxon Vie view Now say X minus y here and now if I use this all right, goes a right lion cock that subtract five and five. And by running I would get 10 and zero just before justice before Now, if I see my Alberto trees that you can see that in the first line we have numbers five and five, some with the result off 10 than five and five, the same parameters. And by subtracting them, we get zero as the result. But was the problem in this? And the problem is that every time we want to create new functional anything in the my calculator class here, we need to duplicate this. For example, if you want another message, I want to play, we need to change this to multiply. We need to do another modification here and Israel to say x time. So I hear we are growing this my calculator class. This is a very bad practice because we have a lot off court duplications and we also violate the open close principle. The open close principle states that a class should be opened for extensions but close for modifications. This class won't be closed for modifications because we need to go ahead and modify the class all the time here. So let's your factor this to use delegates instead. 5. Refactor code to use delegate: And now let's you factor the school to avoid the core duplication. Here. So are some method. Here now has two parameters X and y and we are hard coding this X plus y here and these X minus minus abstract method and so on and so forth. And we want to avoid this and that's what we are going to use the delegate for. So instead of doing this, we'll have another perimeter here and this parameter of you be off the Abdullah gate. This will be our numbers process delegate we created earlier and we'll call this del. So we want hard called this X plus y here materials say Del X and y then that will make our result here and we'll say instead of hard coding the some through our del variable. We have a property called Method off type method info and shoe that we are able to store the name through the name property here. So we also have our method name now, So let's see how we can use this if you go method program that CS you can see that this code won't compile because now we are calling the some method with two parameters. But the sound matter now has a certain paramilitarism. So we need to go ahead and give it an object. Off type numbers process delegates. So we are going to make a new object here and we need to pass in the Method V was before. But since we don't have a method, I just want to go ahead and type some here and by pressing control and point, we can generate a new method here and this method will be automatically created for us. You just get rid of this throwing nothing planted exception. And in this method, we can go ahead and return the Sam off X and Y. Now, if I run this, you can see that we got the same output we have gotten before. If I go to the result that txt there is also the same output as we have seen before. The numbers are five and five. The method name is some and the result is done So basically we don't need to Hard code are functionality in the my calculator class in any off these methods, we can call these through the delegate so actually, if I want to extend my function right. These vets subtract or multiply or any other methods that take two integers and return Annan's. I no longer need to duplicate this court here and extend this file. I can delete this now, and I can simply use this method here with some mattered what we are going to modify now to process numbers here because this method not only returns the result off the calculation, So we're not just calculating, but also looking the out of taxed your file. This would be a little more concise if I would use a longer class to make this logging functionality here just to make it simple. I believe it here for now. So now we would like to extend our functionality with other calculations. We can simply get back to the program that CS and called the process numbers in another delegate. So I am a quickly go ahead and write kind that process numbers. I really walk it to five and five, and I will pass in a new object off this delegate type, and I will passing on diver passing won't apply this time, so at the moment we don't have this matter, but I quickly generated, just like we've seen earlier. And now we are able to right return next time. So I hear. So if I run this program, we will see that we get 10 and 25 now as the result and in our output results Txt you also get five and five and 25 when we are calling Multiply here. So now we could go ahead and write the divide or the soundtrack Method says, Well, just like the some and the multiply method. And we could write it without touching this my calculator process, number function so we can extend our functionality off this My calculator Turkle us by not writing and duplicating court here in the class, but at the place off invocation. We can write our calculation methods, and that's what makes our called very flexible. 6. Adding multiple delegates to the invocation list: Now, let's see how we can add multiple delegates to our invocation list. I have my delegate demo project open if you're working on before. And I extended my program glass with two static methods here. One is called Rights Impulse String on, right. Simply D log message out of the console in a simple way. And the other is called Bright Plants History in here and rise the string in fancy Wait out to the council. Now we onto involved them through the delegate for this small demo. I we just simply comment is called out What we've written before have a press control plus k and see here to common discord. Out I write bomb bleak the gate void. I recall this lager delegates since we will use this for logging and it will have bomb permit or the string log message here Now to you. Was it money to extension this off course? Simple. Unger equals you lock or delegates here. A nail person, right? Symbols drink Now I will copy paste this very quick And let me change this to fantasy. I go here and every person the right fancy string here. As you remember, we need to invoke Tous variables here. But for now we really involved these variables with same string input parameter. So you right string lug message equals we are longing to console do the consortium Okay, Now then walked diarrhea simply right seem longer passing lung message. And I will do this with my fancy longer as well here. So now if I run this program, we'll get We are looking to the console written out two times but first in a simple and then in a fancy way here. And as you can see, we created two instances off this delegate type and through the simple longer were pointing to the right simple string through our fancy longer We are pointing to the right Vance history method But how can we invoke multiple methods through just one delegate by adding the delegates to the invocation list? And this will be very easy to do and four days real simply get rid off our invocation here and also off the fancy luxury created here and now we have only one delegating stay she ation. So I will call this longer delegate or lager Del, because of you have only one variable here and now, by saying longer Del Plus equals well able to add new delegate to our invocation list. I write new longer, delegate and now you're passing my right fancy string here. And of course, this will do nothing. Since we didn't invoke this lager, they'll variable so less weakling walk. It was a longer now long message and now we fire on this. We'll get the same out we have gotten before. So now what happened is that we only have one lager. They'll initialization here we are pointing simple the rights input string but we were able to add a new delegate which points the right fancy string method. So through this laggard l variables through this one variable we were in walking these methods one after the other here. And if I want to invoke another method, I just simply go ahead and write longer. Del plus equals new longer Delegate and I can also go ahead and write something silly here , like right string, read, ask the risks simply I would generate this methods here and now. I write string format zero and our log message, and we still have only this one variable invocation here This one delegating location. So let me just run this very quick. Now I have three methods. School through this fund Invocation here. So that's the basic concept behind the invocation. Honest here. But let me also show you how we can delete a delegate or on subscribe, Subscribe and unsubscribe will be important. Worse when we're talking about events later. So to answer, scribe or to delete a delegate is very easy. Instead, off plus equals we will simply use minus equals here safe. It was minor Sequels for the rice string with Asterix method Our output to be will be the 1st 2 methods cause there is also one more important thing I haven't showed you before but I am sure you will know how this works If I go ahead and instead of blasts equals, I just say equals new lager Delegate fight Francis String and I would change this to blast equals what will be our output. So let me just run this very quickly here. And as you can see, we are not writing the simply string out the console. We are only writing the fancy string and the string with asterisks here because Actually, we're creating new instance off this blogger delegate type and we are pointing to the right symbol string at the moment. But in the next line were saying that this lager they'll variable equals a new longer delegate Andrea pointed of right fancy string here. So you're no longer point to the right simple stream method but to the right France history method and then pointing to this method in the next line, we are also adding this delegate the invocation list. So these two methods here right fancy string and right string with Asterix feel be involved . 7. Delegate inference: in this lecture, I'm going to introduce your concept, which can be sometimes confusing for beginners, C sharp developers. And this concept is called delegate inference. Delegate inference is just simply a great feature that the C sharp compiler provides us and we'll save us a little bit off court here. So in our multicast delegate example, what we had earlier we created a delegate called longer delegate here. And there's remember we can assign methods to this delegate which return void and takes string parameter. But before we were assigning multiple delegates, the invocation list the first great the new instance off this longer delegate type gold lager del, we've wrote new longer delegate and we were pointing to the right simple string method. Then we were saying laggard l plus equals and again we need t. Instance she ate new laggard delegate, which were pointing to another method here. Well, in this case is not that complex when we are looking at this example. But if you jump back to our my calculator initialization here we called this process numbers method and in the search parameter, we need to create a new instance off this delegate type and then pointing to the some method. And at this point, the same tax became a little bit more awkward because we may as the question that why we need to great new instance off this delegate type. Since we know exactly that this process numbers function will take numbers process delegator. But since we want to express ourselves why, according the easiest and simply as this possible, the C Sharp compiler provides us this great feature called Delegate in France. So if you look at this example once again, I make a new instance off this longer delegate type and delegate type in France makes it possible that I don't need to ride this here anymore, since the type will be inferred and this means that the C sharp compiler will figure it out . But this type is and will create a new instance off it behind the scenes for us. So if I just go ahead and delete this right here, our code will be much simply, er because now we are not saying this laggard Alec was a new lager delegate and then pointed the racks input string. But we can go ahead and quickly declare that we are pointing to this right simple string message here. So delegate inference means that the seizure compiler will figure out the type what it needs to create a new instance off. And then we'll also check that the method written here it's comparable to this declaration . What we have here and since this right simple string is a method that takes a string and returns void. It's absolutely perfect for a C sharp compiler because through this lager delegate delegate here we can point to these types off methods. So now we can go ahead and quickly modify this court here as well. I will delete this lager delegate here and we can see that our cold combines perfectly. So now if I around this, you can see that we get the same result as we've gotten earlier. So we are right now just focusing to this are put strings here and the console and we get this. Three are to says we've gotten before, because first we're pointing to the right, seem to string, and then we are adding these two delegates which point to another methods, and so this three methods will be invoked. So now let's be simplified our invocation here. We can also do this Vidar previous process numbers method cause as well. So now we no longer need to create a new instance here. So we can just get rid of this here very quick and we can pass in some which make our court here much more concise and easier to read. Compared to this awkward delegates in Texas reality half here. Now we get rid off this as well and that you run the program once again just to make sure that we get all the functionality we had earlier. And as you can see, 10 and 25 are printing out to the consul. And we also have this tree output texts here. So here you go, delegate in France, the concept which can make our code more easier to read and more easier to use when it comes to using delegates 8. Action and Action of T: So in the last lecture, we have seen how the concept off delegate inference can simplify our code. And in this lecture we'll see how we can make our code even simpler by using the built in delegate types, action and action t in the dotnet framework just to make it simple and easy to understand. I would do the demo in a new project, so I would go here in solution click Add new project, and I would call this action delegate and this will be a council application as well. And I also make this as the start of project here. So I just make sure that this is going to run this program we're running. I also get rid off this using a year because we don't use thes name spaces at the moment. And now let's see how to use the building action delegate. So if you remember in their previous examples, we need to declare a delegate first and then instance she ate it, and the sign methods to it. So let's assume that we now need to declare a custom delegates just we did before because we would like to assign methods that don't take parameter and return void. So let's go ahead and quickly declare Custom Delegate. As in our previous example, go ahead and write Public Delegate, Avoid. Let's go this action that a gate and it won't take anything. And to use it I will go ahead and right Action delegate No equals. And thanks to the delegate inference, we can just quickly ride the method name. The method name will be Action one. And you remember by pressing control point, we can generate a method here. We just quickly get rid of this here and this method. We're just simple right out. Action one is called or action one is involved. So I really hit. See, we constant right line, right? Action one is called instead off Del become also call this action here and now, By invoking this action, we'll see what we got here. Nay, also right. It's Consul Red line here just to make sure that by running the program the consul avant disappear unless I pressed enter. So if I run this, we'll see that we get action. One is called, but it won't surprise us because we are already family. Or with this we have delegating friends, and he also declared a custom delegate just like we did before. But if you want to have the same functionality with the building action, we don't need to declare a custom delegate anymore. And this is also very easy. The only thing we need to do is that instead off writing action delegate, we can write action here and you can see this is a delegate and it's encapsulate method that has no parameters and does not return a value. It's pretty good for us because in fact our action one method is perfectly comparable to this. So we're heat, enter and right action to or has just simply right action. What we had here in the previous line and I were just common this line out. So I call this action and I really assign our action one method that we had before and now a fire on this program. Do you get the same result every as have you gotten before? And what we don't need to do now is that we don't need this declaration here, so I can also delete this right here and those so get rid off this line off course So, as you can see as we go further, we are now expressing ourselves even simpler and simpler now who want to sign another method or maybe had another method to the invocation. A list which is go ahead and write action plus equals right action to And we can generate another method. We can pass in any method that returns void and doesn't have any parameters here. So we can write action to these gold here. Now we're running this. We'll get t two method invocations what we saw earlier and now you can see how this action can save us a little bit off cold and now we don't need to declare the custom delegate itself anymore. But how does action t work? So the action off is the general version off the action here and we can use action t whenever we have one parameter or more parameters and the message itself also need Teoh return void. So in this example, we simply used the action because we don't have any parameter here. But let's be factored is to have a string parameter and then we can use action off the and see how it works. So let's split these strings here into to envy. You see why we are doing this in a moment? I write string text and I will make this another method is via to take string parameter ni right action to and simply con coordinate decks here. And I will do this in the other method, Isabel. So it this will be action one plus dext and just to have the same functionality, our string parameter will be is called. So if I around the program, we will get the same output. Just we have seen just to make it simpler and concise here. But at the moment you can see that we actually have a compiler error here. And it is because our methods have now vom parameter here. And this action is only compatible to method that doesn't have a parameter and return void . And real correct is by using the general question off action. The action off D. So now, because of we have the string parameter, we now need to go ahead and write the generate I parameter here, which will be the type the parameter has. So because our tax parameter re string just simply right string here and this filming that we are using an action off string. So we're assigning methods that has parameters off this type. In this case, one parameter off this type and we return void. And as you can see, it combines perfectly. Now, the only problem with this code is that in order to action be involved, we need to pass in a string parameter here, and this parameter will be is called. Now, if I run this, we can see that we have the same result what we get before and now you can see how action and action off D can substitute the custom delegates here because we no longer need to have these declaration right here. We can just make this in one line and assigned the method as well. 9. Func of T, TResult: So after our action and action of tea example, let's go ahead and see how function delegates work. I create the new project here called Funk Delegate. I made this my start A project and I also put to static methods in this project. Here we have convert to wind one and convert to into and they are converting a string to an integer into different ways. We need to know what is that? In the first case, we are returning directly the value without checking if the string can be converted or not . And this may throw an exception. If the stream cannot be converted, tear in danger. In the second case, we are using the tribe parts methods here. And what this does is that it first checks. If this were you to convert, can be converted to an integer and if it can be, then the result variable, we hold its value. This question mark just simply means that if this state was true, the media return the result. Since we know that if this is true, then the value to convert can be converted to an integer, so we are safe to return the result If not, we're returning zero. So this code ones thrown exception. If the value to comfort cannot be converted to an integer now, if you know what is these methods have string perimeter and also have a return type, and the return type is into in this case, and for these methods, we cannot use the action or action off T any more scenes for those delegates. We can only assign methods that return void. And in this case, we have methods. Let's have returned where you and this means that we need to use another built in delegate type, which is the funk off TT result. If I seem to write funk here, you can see that it has 17 overloads. And this means that, for example, if I just write one type here, then it will mean that we can assign methods to this funk as long as they don't have a parameter and return the type that it's past in here. If I used the second overlord, that will mean that the first I parameter is the time the first parameter need to have and the second is. But the method needs to return, and in this case, you need to use this overload here, since we have to message that. Take one type a string and they return an integer. So if I go ahead and write String in Tier and let's call this converter funk, I can simply make this equal to convert her cover two into one, for example. Now, if I want to use this, I can simply right converter, funk and passing one, for example. And, of course, we need to see the return where you so well was the right console right line here. Now a fire on this program. You get one as the result, because one a string can be seen be converted. But if I want to convert, for example a, then we will get to four month exception here because you want to convert it directly without checking if it's possible or not, and therefore in this example, we can use the convert doing too. That's it here. So if you sign converted into number, you're simply get zero as the result, and we won't get an exceptions thrown here. So that's it. Basically, the only thing you need to remember is that in the general type parameters you can write more parameters. But of course, the message that you assigned to this delegate need to have the same parameters. In this case, we need to have three string incoming parameters and von Return type an integer. Now that we are family or with how funk T t result works, we can jump back to our delegate demo project here. And I will make this as the starter project. And if you just get back to this, I mean, close all documents and I will only open the program that CS And if you jump back to our example, we have delegate here longer delegate which returns void here so we can assign methods that have ah, string parameter and in turn void How can this be substituted? According to the previous lecture, we can simply substitute this with action off t. So we're check various delegate is used and now we get lager. They'll It was right. Simple string. So in order to simplify this, we can simply right action off string seems right. Simple string and the other methods here as well Take one string and return void. It will compile perfectly. And now we can also get rid off this line off court here. And if you take a look at this called that God, that process numbers message here at the search perimeter. We need to pass in. Delegate numbers process, delegate. And these numbers process Delegate is declared in this class here. How can we get rid of this? And now that we know how the funk Off t works, we can simply see that Oh, it has a return type off int and has to in parameters. If it has a return time, we need to use a funk. So we get back to our my calculator class just for a moment. And now here, instead of saying we need a numbers process delegate video, simply say that you need a function that has to into parameters and have an entry turn type , and that's will be our process funk, for example. And if you jump back to the program that C s, we can see that this court complies perfectly. And now we are no longer using this numbers process delegate here, So we can simply go ahead and get rid off this class. So I will delete this year very quick and now you can see that we don't only have two classes in this delegate demo project. And as it's my starter project now, I can simply go ahead and eat a five to run this program. And now you can see that we get the same result as we've gotten earlier. And their code can be even simpler because we don't need Teoh declare any off the delegates here. And there is also other thing we need to notice that our court not just became more simple , but as well. If I go ahead and get my courser to these process numbers now, we can see that this method needs to have two integers and the function and from this function, disability and function delegate, we can now see that Okay, I need to pass in a method that has two integers and returns and end in our previous example. When we used numbers process delegate, we couldn't find this out and we needed to get back to this numbers process delegate class and see the delegate decoration. And I think this can be also very harmful. A lot of times when you have complex delegate usage when developing software