Welcome back.
So let's look at another example of a delegate so that it makes a little more sense before we go into
looking at generics a little bit and then seeing how to make a sorting algorithm, which will be quite
a challenging video, but it's still very useful.
So I'm going to create a public delegate that will be a log handler, and it will just take a string
message.
Very simple stuff.
Then I'm going to have a class here which will be a logger.
Okay.
So this class is supposed to log.
And what it's going to have is two methods.
So one will log to console with that message.
And we're just going to say console dot log or colon or console log.
Maybe it's like this.
And then whatever the message is okay.
So nothing too fancy at this point.
And then let's say we have another method here public void log to let's say file okay.
So here I will also take that message string message.
And I'm going to simulate that I'm logging to file.
So I'm going to say file log and then colon and then whatever the message is okay.
So the idea here really is just to explain how delegates work.
Not so much about the idea of what logging to file means and stuff.
We have seen how logging to a file works, but I'm just going to keep it simple in this case.
So we have this public delegate void log handler.
And this log handler will return nothing, but it requires a string.
So if you want to use the log handler, we need to make sure that the method that we assign to it follows
the same declaration.
We have the same declaration for our log to console as well as log to file.
Okay, so let's use those instead of the main menu.
Our main method I'm going to have a logger which will be a new logger.
And I'm going to have a log handler.
So log handler which will be my log handler to which I assign log to console.
And that will be part of logger okay.
Logger and log to console.
Okay.
So this is my delegate, this log handler.
And you see I can just assign the method without the brackets.
Right.
I'm not calling the method.
I am assigning the method.
And then I can go ahead and use that log handler.
So I can now use this delegate to just say logging to console for example.
Now it will say console log logging to console, whatever.
That's fine.
So now let's run this and see how it works.
And you see console log log into console okay.
Now we can override our log handler with another method.
So the logger to log to file.
So now we're just saying okay there's this other method that also follows the same declaration.
I'm just going to pass that.
And I can use the log handler again and log something.
So log some some stuff.
Okay.
So this stuff will go to get logged onto a file.
At least that's the idea right.
So we run it again and we see the first part.
It logs to the console.
And the second part it logs to the file.
So you see this is where it comes from.
That it logs to the file.
Now obviously this could be the entire logic that we want to use.
And this could be an entirely new logic that we want to use.
So so you see it has some aspects of what we saw inside of using interfaces.
But it's not really that right.
It's something else.
It's really that we have a method that has a certain declaration and we can use to assign other methods
to it with whatever actual implementation we want, and this will become even more interesting and relevant,
and the difference to interfaces will become even more crass and understandable once we use multicast
delegates, which allows us to assign multiple methods to a delegate.
Okay, so this was a very simple example of how we can use delegates.
Now let's go over to the next video.