C# Event Accessors
- By default C# manages the event list automatically for example: the event handlers are added or removed to event invocation list automatically by C#. These types of events are most commonly used in C#.
- Through the use of event accessors we can implement custom event storage mechanism and these accessors give us full control to implements the event handler list.
- There are two types of event accessors which are “add” and “remove”. When a handler is added/registered to an event by using +=, the “add” accessor is called automatically and the “remove” accessor is called when a handler is removed from an event chain, by using -=.
- The “add” and “remove” accessors receive a handler as a parameter, that parameter is a keyword “value”.
- Through event accessors (add and remove), we can define a custom event-handler storage scheme, that means we can manually store event-handlers to an array, queue or a stack.
- The event accessors approach can be used at special situations.
Example:
using System; namespace csharpAdvance { // void type delegate. delegate void ClickHandel(); class CalculatorEvent { // single dimensional array declaration of delegate type. ClickHandel[] clickHandel = new ClickHandel[3]; // Public event ClickTasks of delegate type is declared inside class. public event ClickHandel ClickTasks { add { // add accessor int index; // Loop through 3 times. for (index = 0; index < 3; index++) if (clickHandel[index] == null) { // Assign each event-handler into an array through a keyword “value”. clickHandel[index] = value; break; } // If index variable become 3 then print a message. if (index == 3) Console.WriteLine("Event list is full !"); } // End of add accessor. remove { // remove accessor. int index; for (index = 0; index < 3; index++) if (clickHandel[index] == value) { // Assign null value to each handler which is stored in an array. clickHandel[index] = null; break; } if (index == 3) Console.WriteLine("Event-handler is not available!"); } // End of remove accessor. } // public void type method. public void OnOperations() { for (int index = 0; index < 3; index++) // Check weather an array has handlers. if (clickHandel[index] != null) // If it has handlers then invoke/call each handler. clickHandel[index](); /* else Console.WriteLine("Please register a handler to an event !"); */ } } // Following are different classes with methods. class Add { public void Addition() { Console.WriteLine("Addition event-handler is called."); } } class Substract { public void Substraction() { Console.WriteLine("Substraction event-handler is called."); } } class Multiply { public void Multiplication() { Console.WriteLine("Multiplication event-handler is called."); } } class Divide { public void Division() { Console.WriteLine("Division event-handler is called."); } } class Program { static void Main(string[] args) { CalculatorEvent calculatorEvent = new CalculatorEvent(); Add add = new Add(); Substract substract = new Substract(); Multiply multiply = new Multiply(); Divide divide = new Divide(); Console.WriteLine("Evenets are being added !"); /* Following are different handlers (methods) which is being registered/added to an event-list.*/ calculatorEvent.ClickTasks += add.Addition; calculatorEvent.ClickTasks += substract.Substraction; calculatorEvent.ClickTasks += multiply.Multiplication; calculatorEvent.ClickTasks += divide.Division; // Fire an event through its object reference. calculatorEvent.OnOperations(); Console.WriteLine(); Console.WriteLine("Removing an Addition event-handler"); /* Following handler (method) which is being unregistered/remove from an event-list.*/ calculatorEvent.ClickTasks -= add.Addition; // Fire an event through its object reference. calculatorEvent.OnOperations(); Console.WriteLine(); Console.WriteLine("Adding a Division event-handler"); // Again a handler is added to an event list. calculatorEvent.ClickTasks += divide.Division; // Fire an event again. calculatorEvent.OnOperations(); Console.ReadKey(); } } /* The Output will be: Evenets are being added ! Event list is full ! Addition event-handler is called. Substraction event-handler is called. Multiplication event-handler is called. Removing an Addition event-handler Substraction event-handler is called. Multiplication event-handler is called. Adding a Division event-handler Division event-handler is called. Substraction event-handler is called. Multiplication event-handler is called. */ }