Created
June 28, 2018 09:50
-
-
Save jyotendra/34eb5e5653e5080c24bd5a92ebc1aa51 to your computer and use it in GitHub Desktop.
Super simple delegate and event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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