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 about using delegates in C# to pass around functions like objects.
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
We'll be going over some types in C#
that we can refer to as delegate types.
0:00
Remember that I mentioned that functional
programming considers functions or
0:05
methods to be first class citizens, and
they can be passed around like variables?
0:09
Delegates are the mechanism for
turning methods or
0:14
functions into variables
that can be passed around.
0:16
They are objects that represent
some kind of work to be performed.
0:20
Delegate types have gone
through a lot of evolution and
0:24
improvement through each release of C#.
0:27
Though older types are still there.
0:30
Better ways to achieve the same
goal are available to us in C#.
0:32
When you're working with
older applications,
0:37
you may see the different
ways of using delegates.
0:39
We'll be going over all
the types in this video.
0:42
Starting with the oldest and
0:45
working our way towards more
modern ways of using delegates.
0:46
Then we'll start using them with LINQ.
0:50
We're going to create a little console app
that will give us some like conversation.
0:53
You should follow along by opening
up workspaces with a button below.
0:57
Let's start out by declaring
a delegate in our program class.
1:02
We'll call it SayGreeting, delegate void,
1:06
SayGreeting and then it'll take
string name as a parameter.
1:12
What this does is define a method without
actually giving it any functionality.
1:21
It's like a blueprint or a signature.
1:25
This delegate describes what it will do,
not how it will do it.
1:28
Now we need to write a method
that defines the how.
1:33
We'll write a say hello method that takes
the same type of argument, a string.
1:36
Public static void SayHello and
1:40
it'll take a string name.
1:45
Inside, we'll have the console say hello,
1:52
Console.WriteLine.
1:57
We'll use the string.Format method.
2:00
Hello, I'll stick in the name,
2:05
curly brace and pass in name.
2:10
Now how do we tie these two together?
2:15
In our main method, we'll instantiate
the delegate, SayGreeting and
2:18
tell it that we want to run the method.
2:22
SayHello by passing it.as a parameter.
2:24
Saygreetingsaygreeting =
newsaygreating and
2:30
we point it to say hello.
2:36
This looks a little confusing, right.
2:41
The SayGreeting method is supposed
to take a string as an argument.
2:42
We're actually telling the SayGreeting
delegate to run the say hello method when
2:46
it's called.
2:51
Now we can call SayGreeting like a method,
we need a name though.
2:52
We'll have the console write.
2:57
What's your name?
3:02
And then we'll get the input,
3:07
store it, console.read line.
3:13
Then we'll call the SayGreeting and
pass it the input.
3:20
And I'll save with Ctrl+S.
3:25
Let's run it.
3:28
Down here in my console,
3:29
we need to compile it, mcs Program.cs.
3:32
Then we can run it.
3:37
mano Program.exe.
3:40
What's your name?
3:43
Carling.
3:45
Hello, Carling.
3:45
It worked.
3:47
You might be asking,
why would we do it this way?
3:48
Can't we just call
the SayHello method directly?
3:51
Well, we can write another method that
matches the signature of our delegate but
3:54
provides different functionality.
3:58
Let's write a SayGoodbye method.
4:00
Public static void SayGoodbye.
4:04
String name.
4:12
And I'll do a little copy paste.
4:13
Console But
4:16
then we'll change Hello to Later.
4:22
Then we can point the SayGreeting
delegate to the SayGoodbye method.
4:28
We'll pause with a Console.ReadLine.
4:34
Then SayGreeting = new SayGreeting, and
4:39
we'll point it to the SayGoodbye
method this time.
4:45
And then we'll call it
again with the same input.
4:52
All right, let's see if that works.
4:58
I'm gonna use my up arrow.
5:01
Compile and up arrow two more times.
5:05
What's your name Carling?
5:09
Hello Carling.
5:12
Later Carling, it worked.
5:14
So we use delegates as
a kind of placeholder.
5:17
But this feels pretty clunky, right?
5:20
That's a lot of code to achieve our goal.
5:22
Let's try to clean it up with
some more modern C# features.
5:25
We can instantiate our delegate as we
assign it to the SayGreeting delegate.
5:28
We'll get rid of this and
instead will write delegate,
5:33
and string name has a parameter,
open the curly brace,
5:39
close the curly brace and a semi-colon.
5:44
Then we'll just put the Console.WriteLine
inside of our delegate here.
5:49
We just pointed our delegate
to an anonymous method.
6:00
An anonymous method is a method
that doesn't have a name.
6:03
Its functionality is defined as
we assign it to the delegate.
6:07
We can now delete the SayHello method
because we don't need it anymore.
6:11
Anonymous methods are especially useful
if you only need to run a method once.
6:16
Let's run this to see if it's the same.
6:22
I've saved with Ctrl+S,
6:25
msc Program.cs and mono Program.exe.
6:29
What's your name?
6:34
Carling.
6:35
Yo!
6:38
Later Carling.
6:39
Great.
6:40
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