Using C# Events (for beginners)

There are plenty of books on C# and thousands of topics on C# events/delegates. But nothing can beat Head First C# book by O’Reilly. Its so Fun reading the chapters and they way they explained it. I read the chapter about events and delegates and immediately created a small sample application showing the usage of events.

In this example I’ll build a simple stock quote provider and applications that subscribe to this provider to get stock quote.

The first step is to build the stock quote provider class. The StockQuoteProvider class will have an event “UpdateQuote” and a method “OnUpdateQuote” which will raise the event “UpdateQuote”. The “OnUpdateQuote” method will have a parameter “StockQuoteArgs” parameter. The purpose of “StockQuoteArgs” event argment is to send any arguments to the receiver. Basically we will be sending the Stock Code and the current price of the stock to the receiver.







Create a subscriber method as shown below.








Implement a simple windows application to test the StockQuoteProvider and the Subscribers.






Now run the application to test the Stock Quote Provider application

Sample App





Download the source code from here