Git Product home page Git Product logo

delegatesevents's Introduction

Delegates & Events

Quick demo of how to use delegates and events in C#.

Setting up the event

In our class called UserProcessor.cs we need to set up three things:

  1. The delegate public delegate void UserProcesserEventHandler(object source, EventArgs args);

  2. The Event: public event UserProcesserEventHandler UserProcessed;

  3. The Event Handler notice the virtual keyword

 protected virtual void OnUserProcessed()
        {
            if (UserProcessed != null)
            {
                UserProcessed(this, EventArgs.Empty);
            }
        }

This allows us to fire off notifications by simply calling OnUserProcessed()

Setting up the subscribers

When we want to create a subscriber to the above events we need to abide by the contract defined in the delegate we created on our UserProcessor.cs class For instance our EmailNotificationService class has a method called OnUserProcessed with a signature matching the delegate:

public void OnUserProcessed(object sender, EventArgs args)
        {
            SendEmail();
        }

Wiring it all up

All that's left to do now is have our services subscribed to the events. To see this in action we jump over to our Program.cs file where we have the following code:

  var processor = new UserProcessor();
  var emailService = new EmailNotificationService();
  var textService = new TextMessageNotificationService();
  processor.UserProcessed += emailService.OnUserProcessed;
  processor.UserProcessed += textService.OnUserProcessed;

As you can see we're creating a new instance of everything we need and creating the subscriptions to the events.

Now when we run the application, not only does the message from the UserProcessor.cs show up in the console, but also any message sent by subscribers to the UserProcessed get sent to the console as well.

delegatesevents's People

Contributors

lando786 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.