Delegate and event hierarchy : delegate Event « Language Basics « C# / C Sharp






Delegate and event hierarchy

Delegate and event hierarchy
/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 */
using System;

namespace Client.Chapter_8___Delegates_and_Events
{
  public delegate int MyDelegateEventHandler(MyEventArgs e);
  
  public class MyEventSubscriber
  {
    static void Main(string[] args)
    {
      MyEventPublisher EventPublisher = new MyEventPublisher();
      MyEventArgs MyArgs = new MyEventArgs();

      MyArgs.MyString = "Hello World";
      EventPublisher.MyEvent += new MyDelegateEventHandler(MyHandler);
      EventPublisher.DoSomething(MyArgs);
    }
    static int MyHandler(MyEventArgs e)
    {
      Console.WriteLine(e.MyString);
      return 0;
    }
  }
  public class MyEventArgs: EventArgs
  {
    public int MyInt;
    public long MyLong;
    public string MyString;
  }
  public class MyEventPublisher
  {
    public event MyDelegateEventHandler MyEvent;
    public int DoSomething(MyEventArgs e)
    {
      MyEvent(e);
      return 0;
    }
  }
}

           
       








Related examples in the same category

1.A very simple event demonstrationA very simple event demonstration
2.An event multicast demonstrationAn event multicast demonstration
3.Individual objects receive notifications when instance event handlers are usedIndividual objects receive notifications when instance 
   event handlers are used
4.A class receives the notification when a static method is used as an event handlerA class receives the notification when  
   a static method is used as an event handler
5.Create a custom means of managing the event invocation listCreate a custom means of managing the event invocation list
6.Use the bult-in EventHandler delegateUse the bult-in EventHandler delegate
7.Use delegate: eventUse delegate: event
8.Events:Add and Remove FunctionsEvents:Add and Remove Functions
9.Events:Add and Remove Functions 2Events:Add and Remove Functions 2
10.Events: Custom Add and RemoveEvents: Custom Add and Remove
11.Delegates And EventsDelegates And Events
12.Delegates And Events 2Delegates And Events 2
13.Chaining events.