Let's consider the following example; we will try to create a custom event for a string variable:

  1. Event should be raised whenever the value of string changes.
  2. Event should not be raised if the value is null or the same as the previous value.
  3. Event should provide the following information.
    • Time at valued changed
    • Previous value in the string
    • Current value of string
  1. using System;
  2. usingSystem.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. usingSystem.Collections.Concurrent;
  6. using System.Collections;
  7. namespace Rough_use
  8. {
  9. classProgram
  10. {
  11. public int a = 0;
  12. public static void Main(string[]args)
  13. {
  14. dele newdele = newdele();
  15. newdele.namehaschanged+= new dele.namechange(gottheevent); //subscribing to event published by "dele" class
  16. Console.WriteLine("Haries");
  17. newdele.Riseevent= "Haries";
  18. Console.WriteLine("Vimal");
  19. newdele.Riseevent= "Vimal";
  20. Console.ReadKey();
  21. }
  22. static void gottheevent(Type o, Stringvaluechangedevente) //this is theevent which will be raised when the value of string changes
  23. {
  24. Console.WriteLine(" Time of Changing -" +e.changedat.ToString());
  25. Console.WriteLine(" Current Value set -" +e.currentvalue.ToString());
  26. Console.WriteLine(" Previous Value Overiden -" +e.previous.ToString());
  27. }
  28. }
  29. public class Stringvaluechangedevent : EventArgs //this is the event details will be sent while the value of string "Riseevent" changes
  30. { //EventArgs - is nothing but an empty class from which all event are derived
  31. publicDateTime changedat;
  32. publicstring previous;
  33. publicstring currentvalue;
  34. publicStringvaluechangedevent(DateTime changeda, string previou, stringcurrentvalu) // setting the value to parameter that are sent in event class
  35. { //
  36. this.changedat = changeda;
  37. this.previous = previou;
  38. this.currentvalue = currentvalu;
  39. }
  40. }
  41. classdele
  42. {
  43. public delegate void namechange(Type o, Stringvaluechangedevente); //declaring a simple delegate
  44. public event namechangenamehaschanged; // creating the instant for delegate that is declared
  45. private string riseevent = "";
  46. public string Riseevent
  47. {
  48. get { return riseevent; }
  49. set
  50. { //main story starts here!!!
  51. if (value!= null && value != riseevent) // if the values of string is set null or the value is set to same previous value then we should not rise the event
  52. {
  53. Stringvaluechangedevent e = new Stringvaluechangedevent(DateTime.Now, riseevent, value.ToString());//initiating the value that are sent to along with event
  54. if (namehaschanged != null) //checking whether there are subscriber tothis event
  55. {
  56. namehaschanged(Riseevent.GetType(),e); // rising the event ..ie sent signaling to all subscriber ofthe this event
  57. }
  58. }
  59. riseevent= value; // finally setting the value to string
  60. }
  61. }
  62. }
  63. }
Details

Important

So the subscriber can only subscribe and unsubscribe to my namechange event rather than allowing him to take control over the namechange. The delegate “event” keyword provides extra security to the delegate.