events in c# - geeksforgeeks code example

Example 1: C# events

//1. (Optionally) Make a event class to return data in. Microsoft has a EventArgs class you can derive from, but benifits are minimal besides ahereance to standards.
//2. Make a event class 2 sub scribe to or that will publish the event.
//3. In any place you wish to use the event (subscribe to the event) or subscribe to the event.
    /// 1. An event argument to handle the Data received
    public class YourEventReceiveEventArgs : EventArgs
    {
    	//give out any properties you wish the subscriber to have here.
		//often makeing the set private so the event can be secure is a good idea.
        public byte[] YourDataAsProperties { get; private set; }
        public DateTime DateTime { get; private set; } = DateTime.UtcNow;
        internal DataReceiveEventArgs(byte[] Data){ this.Data = Data; }
    }
	//2. In the Class that will be raise (creates and orignates from) the event add a delegate EventHandler
	public class YourEventPublishingClass
    {
      	//Create a delegate of the signiture of methods you would like to subscribe the event to
      	//Optionally: The object is sender is optional but is a ahereance to standards.
      	//Optionally: Add the event class that you would like to pass data in
		public delegate void YourEventHandler(object Sender, DataReceiveEventArgs e);
		//create Your event using your newly created signature.
    	public event YourEventHandler YourEvent;
      	//Create a way to raise your event this could be a call to check
      	//something durring another in anthor thread or async watch method.
		protected void RaseEvent()
        {
          //if(Condtions are met)
          if(true)
          	handler?.Invoke(this, e);
        }
    }
	//3. Subscribe to event and attach a method to rase on the event
	private class YourSubscriberClass
	{
		//using instance of the class (or you could have a static style class in some rare cases) subscribe
      	//note how you instance and pass the class is up to you.
		private YourEventPublishingClass YourEventPublishingClassProperty;
      	public YourSubscriberClass(YourEventPublishingClass EventClassIn)
        {
      	   YourEventPublishingClassProperty = EventClassIn;
           YourEventPublishingClassProperty.YourEvent += YourEventRecivedMethodEvent;
        }
		//Make sure you your event follows the signature of your delagate
    	private void YourEventRecivedMethodEvent(object Sender, YourEventReceiveEventArgs e)
    	{
      		//replace this with your code
    	}
	}

Example 2: events in c# geeksforgeeks

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace event_programming{    //This is Subscriber Class    class Program    {        static void Main(string[] args)        {            AddTwoNumbers a = new AddTwoNumbers();            //Event gets binded with delegates            a.ev_OddNumber += new AddTwoNumbers.dg_OddNumber(EventMessage);            a.Add();            Console.Read();        }              //Delegates calls this method when event raised.          static void EventMessage()        {            Console.WriteLine("********Event Executed : This is Odd Number**********");        }    }    //This is Publisher Class    class AddTwoNumbers    {                public delegate void dg_OddNumber(); //Declared Delegate             public event dg_OddNumber ev_OddNumber; //Declared Events         public void Add()        {            int result;            result = 5 + 4;            Console.WriteLine(result.ToString());            //Check if result is odd number then raise event            if((result % 2 != 0) && (ev_OddNumber != null))            {                ev_OddNumber(); //Raised Event            }                  }    }}