Welcome back.
In this video we're going to start looking at events.
And events are going to be something that will follow us for a bit more than just this one video, but
it's really important.
It's really powerful as well, and it's a core concept that builds on top of delegates.
So that's why it was important for you to understand the basics of delegates before we jump into events.
So what are events?
An event is a way for a class to notify other classes or objects when something of interest occurs.
Events are based on delegates.
That's why we need delegates, right?
Then why would we use events?
Events enable a class to provide notifications without knowing which other class or object will receive
those notifications.
And this promotes loose coupling and separation of concerns.
So that's really cool because you can basically write code that then will go ahead and execute whatever
another piece of code decided.
Something needs to be executed and it doesn't know this other code doesn't know that you are listening
to that code, and you are waiting for it to do something so that you can do something that's really
cool.
And when would you use an event?
Well, when you need a mechanism for one object to notify other objects about changes or actions, and
where will you see events well commonly used in scenarios like logging monitoring, data changes, button
clicks, keyboard clicks and any situation where notification mechanism is required.
So let's go ahead and build our first little event.
And therefore we need a delegate first.
So first let's create a public delegate.
And this one will return nothing and will be called notify.
All right.
And it will just notify with a message.
Very simple stuff.
We have seen that multiple times before.
Right.
So an event is based on the delegates that define the signature of the methods that will then handle
the event.
Okay.
So this is the signature of the methods that should handle the the event.
Now let's create a class that can publish events.
So basically where events start to happen.
So public class and this one will be the event publisher.
And we are going to create a public event.
And that one will be called will be of type notify on notify.
So you already see the keyword on here.
And this on is actually important.
So this is not arbitrary.
It's really important because it indicates that we're talking about an event.
So this one will be executed when the event is triggered.
So it signifies that the method is not just a regular method but one that is called when a specific
event occurs.
Okay.
So now let's go ahead and just have a method can raise that event.
So I'm going to call this raise event.
So in raising an event means that you are basically starting the event.
You're saying hey event.
Let's go okay.
And and this raise event you can see it also returns nothing.
And it takes in a string or the parameter is a string.
So here what we're going to do is we're going to use on notify.
And we are going to invoke.
And this invoke method will invoke the event if there are any subscribers.
And that's why we have this question mark here.
So this on notify could basically have no subscribers.
So nobody could listen to that event or wait for that event to trigger and do something once that event
is triggered.
That's why it only invokes so.
It only executes the notify delegate when there are subscribers to the event.
So how can we now make sure that we subscribe to the event.
And therefore we're going to create another class.
So public class event subscriber.
And now this class here will just have a public void on event raced method and a string message.
And here I'm just going to write onto the console something like event received.
And then whatever the message was okay.
Plus message.
So that's our event subscriber here we have our event publisher.
We can now notify.
We can raise the event and which will invoke the on notify whichever events are in there.
So are waiting for the event to trigger.
And now inside of our main method, let's go ahead and use our event publisher and our event subscriber.
Event subscriber like so.
And and then use our publisher with its own notify.
You can see our notify now has this flash icon here.
So this indicates that we're talking about an event okay.
So this is on notify event.
And we are going to subscribe to that event by using our subscriber dot on event raced.
and this is just going to be one subscriber.
Obviously we could have multiple subscribers and now our publisher can just raise the event whenever
something occurs.
So we can just test run it.
Okay.
And here I'm going to have the read key code so that my code doesn't stop read key.
Now let's run this and we should see that our subscriber is informed about the whole thing going on,
the event going on and it says event received test.
So a lot of complicated code just to set up an event and basically just to write something onto the
console.
And now you might wonder why would this matter?
Why would you care?
Creating an event if you just want to execute code?
Well, the thing is that all of this stuff could be events that we didn't write ourselves, but we can
subscribe to those events and we can now be notified whenever that happens.
So, for example, whenever the user is pressing a key on the keyboard, that could be an event that
triggers something.
So let's quickly summarize what we saw.
The thing is this is really just for you to understand the syntax.
It's not just yet about seeing real world examples.
We're going to start doing that in the next video.
We're going to build a little more of a real world example.
But generally the structure is if you want to create your own events and your own listeners, what you
have to do or subscribers, you have to set up your delegate with the method declaration you want your
events to use.
So then we have this on keyword whenever we set up an event using our delegate.
So you see we have this public keyword so that we can use the event from the outside of this class we
have the event keyword.
And then we are using our delegate that we created earlier on.
and we give it a name that starts with on.
We also inside of our event publisher, we need to raise the event.
And this raised event you see follows the same structure.
And then here we are using the on notify which can be null, which is why we're using nullable.
And we're invoking whatever listeners there are.
So we're invoking all of the listeners.
We saw that earlier in Multicast Delegate, where we had multiple invocation items, and we invoked
them one by one.
And now you see that we're basically doing the same thing here.
We're invoking all of the items, all of the subscribers that are listening to my on notify.
So here we are preparing an event subscriber.
And we're saying what should happen whenever that event is raised.
And you see it's the same structure it's void on event raised string message.
So the same declaration as our delegate.
And in here we just use our code whatever we want to happen when the code happens.
So in this case we're just notifying on the console.
And then we're preparing the publisher.
We're preparing the subscriber.
We're setting up the publisher on notify and subscribe with our event raised method that should be called
whenever the event is raised.
So on event raised, then we want to execute whatever we defined in here and that will then be called
once the raise event of the publisher method is called.
So this one right here.
Once this method is called then we're raising the event and we're executing whatever other methods are
listening to us.
So if we were listening like if let's say these are two different classes that are now listening and
waiting for the event to happen.
Now once that happens, you see both events were received and test was executed.
Okay, I'd say that's it for this video.
In the next video we're going to look at the real world example.