Skip to content

Instantly share code, notes, and snippets.

@jyotendra
Created June 28, 2018 09:50
Show Gist options
  • Save jyotendra/34eb5e5653e5080c24bd5a92ebc1aa51 to your computer and use it in GitHub Desktop.
Save jyotendra/34eb5e5653e5080c24bd5a92ebc1aa51 to your computer and use it in GitHub Desktop.
Super simple delegate and event
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Yo Buddy");
Subscriber myEventSubscriber = new Subscriber();
Publisher myPublisher = new Publisher();
myPublisher.PublishWordsToPrint += new Publisher.SimplyPrint(myEventSubscriber.PrintMessageFromPublisher);
myPublisher.ProcessEvent("Hello world!!");
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class Publisher
{
public delegate void SimplyPrint(string words);
public event SimplyPrint PublishWordsToPrint;
public void ProcessEvent(string wordsToPrint)
{
if(PublishWordsToPrint != null)
{
PublishWordsToPrint(wordsToPrint);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class Subscriber
{
public void PrintMessageFromPublisher(string message)
{
Console.WriteLine(message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment