using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleAdvancedTopics { // Chapter 8 // define a delegate type public delegate void MessageHandler(string message); // class Publish "Publisher" public class Publisher { // Declare an event using a delegate type public event MessageHandler MessagePublished; public void PublishMessage(string message) { Console.WriteLine($"Message Published: {message}"); // Raise the Event OnMessagePublished(message); // define the method } // PublishMessage // OnMessagePublished // Protected virtual method to raise the event protected virtual void OnMessagePublished(string message) { MessagePublished?.Invoke(message); } // OnMessagePublished } // Publisher // class Subscribe "Subcriber" public class Subcriber { public void HandleMessage(string message) { Console.WriteLine($"Subscriber says: Message {message} receieved"); } } // Subscriber // Main Class class DelegatesAndEvents { } // DelegatesAndEvents }