1 00:00:00,350 --> 00:00:04,640 Let's move on to another type of delegate in C#, Actions. 2 00:00:04,640 --> 00:00:09,780 Actions are a type of generic delegate that can be declared at instantiation. 3 00:00:09,780 --> 00:00:14,460 Remember that generic means that you need to specify a type when you declare it, 4 00:00:14,460 --> 00:00:16,950 like we did with lists earlier. 5 00:00:16,950 --> 00:00:21,190 Actions always return void and can take multiple parameters. 6 00:00:21,190 --> 00:00:25,330 How about we look at the documentation before writing an action? 7 00:00:25,330 --> 00:00:31,452 We'll type action.net. 8 00:00:31,452 --> 00:00:32,092 There it is. 9 00:00:34,739 --> 00:00:39,890 Encapsulates a method that has a single parameter and does not return a value. 10 00:00:39,890 --> 00:00:44,120 This specific method signature is an action that takes a single parameter. 11 00:00:44,120 --> 00:00:46,150 The T here, in angle brackets, 12 00:00:46,150 --> 00:00:50,240 represents the type parameter we need to specify when we declare the action. 13 00:00:51,280 --> 00:00:54,880 Check out these other versions of the same method on the left here. 14 00:00:54,880 --> 00:00:56,670 If you need more than one parameter, 15 00:00:56,670 --> 00:01:00,230 you'll need to specify each one at declaration, separated by a comma. 16 00:01:01,330 --> 00:01:02,670 Let's get back to work spaces. 17 00:01:04,150 --> 00:01:06,711 We can get rid of our delegate declaration, SayGreeting. 18 00:01:09,120 --> 00:01:11,989 Then, we can declare an action here in main. 19 00:01:14,239 --> 00:01:19,950 Action, and it'll take a string parameter inside angle brackets and 20 00:01:19,950 --> 00:01:22,580 we'll call it, sayGreeting. 21 00:01:25,140 --> 00:01:29,090 then we can assign our anonymous method to this say greeting. 22 00:01:30,570 --> 00:01:33,758 Since we got rid of our delegate declaration, we'll need to change our 23 00:01:33,758 --> 00:01:36,870 SayGoodbye functionality to use another anonymous method. 24 00:01:38,380 --> 00:01:46,660 I'll do a copy, paste And I'll change that to Later. 25 00:01:48,420 --> 00:01:53,700 Now the sayGreeting action is pointing to different anonymous methods in our program 26 00:01:53,700 --> 00:01:56,490 and it's a little cleaner than when we were using the delegate keyword. 27 00:01:58,000 --> 00:01:59,950 We can get rid of our sayGoodbye method here. 28 00:02:03,400 --> 00:02:04,392 Let's see if this works. 29 00:02:04,392 --> 00:02:10,538 Compile wirh mcs Program.cs and 30 00:02:10,538 --> 00:02:14,495 mono Program.exe. 31 00:02:14,495 --> 00:02:16,336 What's your name? 32 00:02:16,336 --> 00:02:17,690 Carling. 33 00:02:17,690 --> 00:02:18,654 Hello Carling. 34 00:02:18,654 --> 00:02:20,140 Yo. 35 00:02:20,140 --> 00:02:20,900 Later Carling. 36 00:02:20,900 --> 00:02:22,750 Great, it still works. 37 00:02:22,750 --> 00:02:25,628 So what if we need a delegate that returns something? 38 00:02:25,628 --> 00:02:30,793 Actions don't return anything, that's where the funk type comes into play. 39 00:02:30,793 --> 00:02:35,820 The funk type is a delegate that works like an action, but has a return value. 40 00:02:35,820 --> 00:02:44,120 Let's take a quick look at the func documentation, func.net. 41 00:02:44,120 --> 00:02:44,690 Here it is. 42 00:02:47,578 --> 00:02:51,510 So with a func, the first parameter is the in parameter and 43 00:02:51,510 --> 00:02:53,860 the second one is the out. 44 00:02:53,860 --> 00:02:57,280 You can see down here, it says in and out. 45 00:02:57,280 --> 00:03:01,350 If you look at the other ones here on the left, it's got more methods for 46 00:03:01,350 --> 00:03:04,030 when you need more than one input parameter, but 47 00:03:04,030 --> 00:03:06,410 the output is always the last type parameter. 48 00:03:07,550 --> 00:03:11,744 Let's get back to work spaces and we'll create a func that will write a message to 49 00:03:11,744 --> 00:03:14,225 the console and return the input from the user. 50 00:03:18,329 --> 00:03:23,180 Func and the type parameters will be string. 51 00:03:23,180 --> 00:03:30,530 So the in parameter is a string and the out parameter is a string, 52 00:03:30,530 --> 00:03:35,704 and we'll call it conversate = an anonymous 53 00:03:35,704 --> 00:03:39,668 method delegate string message. 54 00:03:39,668 --> 00:03:44,683 So, that's our input parameter and 55 00:03:44,683 --> 00:03:51,154 then we'll write the message to the console and 56 00:03:51,154 --> 00:03:56,170 then we'll return whatever the user 57 00:03:56,170 --> 00:04:00,230 enters Console.ReadLine. 58 00:04:03,018 --> 00:04:06,623 Okay, we can use this function to get and store the name. 59 00:04:08,523 --> 00:04:14,620 So down here, we'll take out string input and we'll stick it up here. 60 00:04:14,620 --> 00:04:18,409 So string input = conversate and 61 00:04:18,409 --> 00:04:23,822 we'll pass it ("what's your name?") and 62 00:04:23,822 --> 00:04:31,770 then we can get rid of this ("what's your name?") here. 63 00:04:31,770 --> 00:04:35,470 You see how we're passing in a string to the sayGreeting action? 64 00:04:35,470 --> 00:04:38,850 We don't actually have to do that because the variables we declare 65 00:04:38,850 --> 00:04:43,430 outside of the function are in the same scope as the body of the function. 66 00:04:43,430 --> 00:04:45,300 So we can change its name to input. 67 00:04:47,200 --> 00:04:49,990 We don't need the name parameter anymore, but 68 00:04:49,990 --> 00:04:53,720 we could re-factor this function to suit both of our greetings. 69 00:04:53,720 --> 00:04:57,240 We'll change the parameter to be greeting and 70 00:04:57,240 --> 00:04:59,750 we can use it for both hello and goodbye. 71 00:05:01,130 --> 00:05:06,630 And then I'll copy that greeting, put it here and 72 00:05:06,630 --> 00:05:09,389 now we can use that for both saying hello and goodbye. 73 00:05:11,900 --> 00:05:17,470 So down here instead of input, you can say Hello, and 74 00:05:23,150 --> 00:05:27,533 we can get rid of all of this right here and 75 00:05:27,533 --> 00:05:34,650 then, Later. 76 00:05:34,650 --> 00:05:37,996 Let's add some other conversation lines to make it a little more interesting. 77 00:05:37,996 --> 00:05:43,228 So conversate, we'll pass, 78 00:05:43,228 --> 00:05:48,260 ("Nice to see you:") and 79 00:05:48,260 --> 00:05:51,279 then we'll say, 80 00:05:51,279 --> 00:05:59,139 conversate,("Are you doing well?"). 81 00:05:59,139 --> 00:06:02,521 Now let's run it, and see if it works. 82 00:06:02,521 --> 00:06:07,146 Mcs Program.cs and 83 00:06:07,146 --> 00:06:12,070 mono Program.exe. 84 00:06:12,070 --> 00:06:13,030 What's your name. 85 00:06:13,030 --> 00:06:13,580 Carling. 86 00:06:14,670 --> 00:06:15,310 Hello Carling. 87 00:06:15,310 --> 00:06:16,760 Nice to see you. 88 00:06:16,760 --> 00:06:17,954 You too. 89 00:06:17,954 --> 00:06:19,255 Are you doing well? 90 00:06:19,255 --> 00:06:20,439 Yep. 91 00:06:20,439 --> 00:06:21,959 Later Carling. 92 00:06:21,959 --> 00:06:23,352 Awesome.