Publisher And Subscriber In C#

Note: Before reading this article you must have an understanding of Delegates and Events first. Just a basic understanding!

Delegates

Delegates holds the reference of the Method. It specifies the signature of a method, and when you have a delegate instance, you can make a call to it as if it was a method with the same signature.

Events

Everyone is waiting for their salaries, so debit of your salary into your account is any event. The triggers on 31st or 30th in IT industry of each month. And we are the subscribers who have been paid for their work or efforts.

Here is our main content:

Publisher and subscriber

Three main actors of Publisher and Subscriber pattern:

  • The Event: Salary Debited in your account in an event “SalaryDebited”.
  • The Publisher : The event is published by the “Organization” class.
  • The Subscriber: Each Employee subscribe to “SalaryDebited” event.

Assignment

Let's say we have Windows Form (UserForm) and we have a Grid on our “UserForm”. Again we have one “UserControl” and on UC we have one button.

Now what my job is, on click of button I need to fill the Grid on user form.

So in our case the Publisher is User control and Subscriber is windows form.

Output

  1. This is our form and its description. We are adding control (PopulateGrid) on Panel.

    PopulateGrid

  2. Grid Populated with data on button click:

    Grid

Happy coding!