https://stackoverflow.com/a/73819648/3063273

CSharp · Console

/Program.cs

using System;
using System.Threading;
using System.Threading.Tasks;
					
public class Program
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
	}
}

record NotificationEvent(DateTime NotificationDate, string NotificationMessage);

class Publisher{
    
	//publishers name
	public string PublisherName { get; private set; }

	//publishers notification interval
	public int NotificationInterval { get; private set; }

	// declare a delegate function
	public delegate void Notify(Publisher p, NotificationEvent e);

	// declare an event variable of the delegate function
	public event Notify OnPublish;

	// class constructor
	public Publisher(string _publisherName, int _notificationInterval){
		PublisherName = _publisherName;
		NotificationInterval = _notificationInterval;
	}

	//publish function publishes a Notification Event
	public void Publish(){

		while (true){

			// fire event after certain interval
			Thread.Sleep(NotificationInterval);

			if (OnPublish != null)
			{
				NotificationEvent notificationObj = new NotificationEvent(DateTime.Now, "New Notification Arrived from");
				OnPublish(this, notificationObj);
			}
			Thread.Yield();
		}
	}
}

class Subscriber{
    
	public string SubscriberName { get; private set; }

	public Subscriber(string _subscriberName){
		SubscriberName = _subscriberName;
	}

	// This function subscribe to the events of the publisher
	public void Subscribe(Publisher p){

		// register OnNotificationReceived with publisher event
		p.OnPublish += OnNotificationReceived;  // multicast delegate 

	}

	// This function unsubscribe from the events of the publisher
	public void Unsubscribe(Publisher p){

		// unregister OnNotificationReceived from publisher
		p.OnPublish -= OnNotificationReceived;  // multicast delegate 
	}

	// It get executed when the event published by the Publisher
	// ***I want to make this method async here:***
	protected virtual /**/ async /**/ void OnNotificationReceived(Publisher p, NotificationEvent e){
		await Task.Delay(TimeSpan.FromSeconds(1));
		Console.WriteLine("Hey " + SubscriberName + ", " + e.NotificationMessage +" - "+ p.PublisherName + " at " + e.NotificationDate);
	}
}
;