This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Learn how to use the delegate types Action and Func.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's move on to another type
of delegate in C#, Actions.
0:00
Actions are a type of generic delegate
that can be declared at instantiation.
0:04
Remember that generic means that you need
to specify a type when you declare it,
0:09
like we did with lists earlier.
0:14
Actions always return void and
can take multiple parameters.
0:16
How about we look at the documentation
before writing an action?
0:21
We'll type action.net.
0:25
There it is.
0:31
Encapsulates a method that has a single
parameter and does not return a value.
0:34
This specific method signature is
an action that takes a single parameter.
0:39
The T here, in angle brackets,
0:44
represents the type parameter we need
to specify when we declare the action.
0:46
Check out these other versions of
the same method on the left here.
0:51
If you need more than one parameter,
0:54
you'll need to specify each one at
declaration, separated by a comma.
0:56
Let's get back to work spaces.
1:01
We can get rid of our delegate
declaration, SayGreeting.
1:04
Then, we can declare
an action here in main.
1:09
Action, and it'll take a string
parameter inside angle brackets and
1:14
we'll call it, sayGreeting.
1:19
then we can assign our anonymous
method to this say greeting.
1:25
Since we got rid of our delegate
declaration, we'll need to change our
1:30
SayGoodbye functionality to
use another anonymous method.
1:33
I'll do a copy, paste And
I'll change that to Later.
1:38
Now the sayGreeting action is pointing to
different anonymous methods in our program
1:48
and it's a little cleaner than when
we were using the delegate keyword.
1:53
We can get rid of our
sayGoodbye method here.
1:58
Let's see if this works.
2:03
Compile wirh mcs Program.cs and
2:04
mono Program.exe.
2:10
What's your name?
2:14
Carling.
2:16
Hello Carling.
2:17
Yo.
2:18
Later Carling.
2:20
Great, it still works.
2:20
So what if we need a delegate
that returns something?
2:22
Actions don't return anything, that's
where the funk type comes into play.
2:25
The funk type is a delegate that works
like an action, but has a return value.
2:30
Let's take a quick look at
the func documentation, func.net.
2:35
Here it is.
2:44
So with a func, the first
parameter is the in parameter and
2:47
the second one is the out.
2:51
You can see down here, it says in and out.
2:53
If you look at the other ones here on
the left, it's got more methods for
2:57
when you need more than
one input parameter, but
3:01
the output is always
the last type parameter.
3:04
Let's get back to work spaces and we'll
create a func that will write a message to
3:07
the console and
return the input from the user.
3:11
Func and
the type parameters will be string.
3:18
So the in parameter is a string and
the out parameter is a string,
3:23
and we'll call it
conversate = an anonymous
3:30
method delegate string message.
3:35
So, that's our input parameter and
3:39
then we'll write the message
to the console and
3:44
then we'll return whatever the user
3:51
enters Console.ReadLine.
3:56
Okay, we can use this function to get and
store the name.
4:03
So down here, we'll take out string
input and we'll stick it up here.
4:08
So string input = conversate and
4:14
we'll pass it ("what's your name?") and
4:18
then we can get rid of this
("what's your name?") here.
4:23
You see how we're passing in
a string to the sayGreeting action?
4:31
We don't actually have to do that
because the variables we declare
4:35
outside of the function are in the same
scope as the body of the function.
4:38
So we can change its name to input.
4:43
We don't need the name parameter anymore,
but
4:47
we could re-factor this function
to suit both of our greetings.
4:49
We'll change the parameter
to be greeting and
4:53
we can use it for both hello and goodbye.
4:57
And then I'll copy that greeting,
put it here and
5:01
now we can use that for
both saying hello and goodbye.
5:06
So down here instead of input,
you can say Hello, and
5:11
we can get rid of all
of this right here and
5:23
then, Later.
5:27
Let's add some other conversation lines
to make it a little more interesting.
5:34
So conversate, we'll pass,
5:37
("Nice to see you:") and
5:43
then we'll say,
5:48
conversate,("Are you doing well?").
5:51
Now let's run it, and see if it works.
5:59
Mcs Program.cs and
6:02
mono Program.exe.
6:07
What's your name.
6:12
Carling.
6:13
Hello Carling.
6:14
Nice to see you.
6:15
You too.
6:16
Are you doing well?
6:17
Yep.
6:19
Later Carling.
6:20
Awesome.
6:21
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up