Welcome back.
In this video we're going to look at multicast delegates.
Now that you understand how general delegates work.
So in C sharp multicast delegates are delegates that hold references to more than one method.
When they are invoked, they call all the methods they reference in the order they were added.
Why would you use multicast delegates?
Basically to allow a single delegate to invoke multiple methods?
This is useful for implementing event handling, where multiple listeners need to respond to the same
event.
And when would you use it?
Well, you use multicast delegates when you need multiple methods to respond to a single event or action,
and this is commonly used in the event handling.
For example, for notification systems and scenarios where multiple actions need to be performed in
response to a single trigger.
That's the key here.
So let's see this in practice.
Therefore we have our log handler from earlier right.
So we have our public void log handler with the logger with the log two console as well as log to file
methods.
Now we have our log handler to which we added the log to console okay.
Now instead of calling my log handler directly, what I'm going to say is I'm going to add something
else to the log handler by using the plus equal, and I'm going to use logger dot log to file.
So this already makes our log handler a multicast delegate because it has two methods assigned to it.
One is the log to file and the other one is the log to console.
So this plus equal is just saying add to the old method that is already in there, or the methods that
are already in there.
Add another one.
So add the log to file that will be executed whenever the log handler is executed.
So let's invoke the log handler by passing a string message to it.
Log this info.
Okay for example.
So very simple.
Let's run this and we will see that it says console log.
Log this info and file log log this info.
So the cool thing is now that it logged it to multiple locations.
And this could be logging it to many different areas.
So we could look it to the display or to the, uh, log inside of our debugger.
We could log it to a file.
We could do also so many different things that happen once our log handler is executed, once a detail
is passed to it.
So this is just a very simple example of how multicast delegates work.
So again in these two lines we are creating a multicast delegate.
And here we are invoking the multicast delegate.
Now, if you want to remove a method from the multicast delegate, you can do so in a similar fashion
as you can add it.
So you can just say log handler minus equal.
And then we can just say logger dot and delete the method that we want to delete.
So let's say we get rid of the log to file method.
And now we can use our log handler once again mojito.
And we can say after removing log to file okay.
So let me do that.
Like so let's run this again and we will see console log this info.
So console log log this info file log log this info.
And then only console log which says after removing log to file, because we have gotten rid of the
log to file method, it's not being called anymore because it's not part of the overall execution of
our multicast delegate.
Now we do get an error here where it says when you hover over it, log handler is not null here.
So not really a problem but still possible null reference assignment.
And here it says a log handler may be null here because there was something deleted from log handler.
So a method could be or was deleted.
So it could very well be that there is no more method listening to this log handler anymore, or not
assigned to the log handler anymore, and in that case it could run into an error.
So how can we fix that?
Well, the best practice would be to really make sure that we are invoking safely.
So let me create a quick method or actually let's do that in the next video okay.
See you there.