I am currently having a hardtime understanding and implementing events in C# using delagates. I am used to the Java way of doing things:
- Define an interface for a listener type which would contain a number of method definitions
- Define adapter class for that interface to make things easier if I'm not interested in all the events defined in a listener
- Define Add, Remove and Get[] methods in the class which raises the events
- Define protected fire methods to do the dirty work of looping through the list of added listeners and calling the correct method
This I understand (and like!) - I know I could do this exactly the same in c#, but it seems that a new (better?) system is in place for c#. After reading countless tutorials explaining the use of delegates and events in c# I still am no closer to really understanding what is going on :S
In short, for the following methods how would I implement the event system in c#:
void computerStarted(Computer computer);
void computerStopped(Computer computer);
void computerReset(Computer computer);
void computerError(Computer computer, Exception error);
^ The above methods are taken from a Java application I once made which I'm trying to port over to c#.
Many many thanks!
Thank you all so much for your answers! Finally I'm starting to understand what is going on. Just one thing; It seems that if each event had a different number/type of arguments I'd need to create a different :: EventArgs class to deal with it:
This would require three classses to deal with the events!? (Well two custom, and one using the default EventArgs.Empty class)
Cheers!
Ok, FINAL clarification!: So this is pretty much the best I can do code-wise to implement those events?
You'd create four events, and methods to raise them, along with a new EventArgs-based class to indicate the error:
Classes would then subscribe to the event using either a method or a an anonymous function:
A few things to note about the above:
delegate{}
piece on each event declaration is just a trick to avoid having to do a null check. It's subscribing a no-op event handler to each eventSee my events/delegates article for much more detail on events.
The main difference is that in C# the events are not interface-based. Instead, the event publisher declares the delegate which you can think of as a function pointer (although not exactly the same :-)). The subscriber then implements the event prototype as a regular method and adds a new instance of the delegate to the event handler chain of the publisher. Read more about delegates and events.
You can also read short comparison of C# vs. Java events here.
there are several ways to do what you want. The most direct way would be to define delegates for each event in the hosting class, e.g.
any object may 'listen' for this event simply by:
The 'recommended standard way' of doing this would be to define a subclass of EventArgs to hold the Computer (and old/new state and exception) value(s), reducing 4 delegates to one. In this case that would be a cleaner solution, esp. with an Enum for the computer states in case of later expansion. But the basic technique remains the same:
listeners are removed using the -= syntax instead of +=
You'll have to define a single delegate for that
ComputerEventArgs would be defined like this:
The class that fires the events would have these:
This is how you assign handlers to the events:
Your handler is: