Welcome back.
So in this video we're going to cover delegates.
And at least we're going to start covering them.
They are a little more complicated.
So there is a lot more to know about them than just what we can cover in one video.
And we have seen delegates just a tiny bit when we were using predicates.
And predicates are just a tiny portion of delegates.
But in this video, we're going to go deeper into delegates, and then we're going to see how multicast
delegates work.
And then we're going to see events.
And delegates are really powerful when it comes to events, but they're also powerful in other situations.
And let's look at those.
So what is a delegate?
A delegate is a type that represents references to methods with a specific parameter list and return
type.
Why would you use a delegate?
Well, delegates provide a way to pass methods as parameters, enabling flexible and extensible code
designs.
So you cannot just pass variables as parameters as we've seen many times before.
But you can actually actually also pass methods themselves using delegates.
So when would you use delegates?
You use delegates when you need a way to encapsulate a method and pass it as an argument, as we just
saw, so to speak, or heard?
And where would you use delegates?
Well, delegates are used in callback mechanisms.
Event handling and designing flexible APIs and APIs are application programming interfaces, and they
allow for an easier communication between different programs, which is very, very useful because you
don't want to make your application fully closed and dependent on you coding everything, but you also
want to be able to use other applications and work together with them efficiently in order to you not
having to build everything yourself.
And that will become a lot more relevant later on in the course.
And we're going to see how to use APIs as well.
Okay.
So now let's look at how to actually use a delegate.
And we've seen it a little bit with the predicate.
But now let's really dive deeper.
So first of all how do we declare a delegate.
So first it's really useful to understand that delegates define a method signature.
And any method assigned to a delegate must match this signature.
Okay.
now let's start with the declaration of such a delegate.
So I'm going to put it here because otherwise I can't do it properly.
So I will make it public so that I can access it from anywhere.
Then I use this delegate keyword and then I define the entire method signature.
Okay.
So for example, if I want the methods that can be passed into the delegate to return nothing, I can
use void.
If I wanted it to return a string, I can use string and so forth.
So I'm going to keep it simple.
So I'm just going to say this will be a delegate that I'm going to call notify.
And you see I used a capital N letter here.
And then inside of the brackets is where I define which parameters I want to have.
So I'm just going to say I want to have a message okay.
So that's it I end with a semicolon here.
You see there is no body for the delegate.
So even though it looks pretty much like a method all the way up to this point, once we add the delegate
keyword, there is no body, so I do not add a body to a delegate.
Okay, so now we have this notify delegate.
That's where we are with the declaration.
So how can we instantiate a delegate.
Therefore we need to make sure that we have a method that we can pass into the delegate.
So there are multiple ways to do it.
Let's say we have this method here.
So static void and this one will be show message.
And it will also take a message and it will display the message okay.
So very simple static method or void method okay.
Now let's use this method to do the instantiation of our delegate.
So I'm going to use notify.
Then I give this delegate a name I'm going to call this notify delegate.
And then I can do it two different ways.
So either I just directly add the show message in here.
Like so.
Or another way would be notify.
Notify delegate equals to new notify and notify delegate.
So this would be another way.
This is the old school way how you did it before C sharp two like super old school way.
But it's still is valid and still is something that you will likely see in your journey as a developer.
But this is another like more convenient way where you basically just pass in the method itself to the
delegate.
Okay, so this is how the instantiation works.
And then how do we do the invocation.
So basically calling the delegate.
Well we can just use the name of the delegate and then pass whatever the parameter definition of the
delegate is in here.
So you see notify expects a message of type string and will return a void.
So it will return nothing.
So I can just say hello delegates and maybe add a comma here for grammatical correctness.
And then we finish with a semicolon.
So this right here is the simplest way of the three steps.
So declaration and instantiation and invocation.
Now if we run this we will see that it will just say hello delegates.
Nothing too fancy.
Okay, so what I'm doing is I'm declaring a delegate and it has a certain method signature.
So this is the method signature return type name doesn't matter but then parameters okay.
So then and here I am assigning a method to the delegate that that matches the method signature.
So here you see void string message.
The name message doesn't matter.
But it's what does matter is the string.
Here we can very easily test if it still works even though we have a different name.
This is always just something you can check out.
So you see the naming itself is not relevant.
It's the type right?
The parameter type as well as the return type.
Okay.
The naming of those variables is not as relevant okay.
Okay, so that's pretty much it.
For the first step of delegates we're going to dive deeper into it.
They're really important when it comes to events.
So we need to understand multicast delegates for events.
But before we do that let's look a little more into delegates because this was a very simplistic example.
I want to expand the complexity of the example.
So you still get a better feel for how the delegates work.
All right.
So see you in the next video.