Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
EventHandler(TEventArgs) Delegate
Collapse the table of content
Expand the table of content

EventHandler<TEventArgs> Delegate

Represents the method that will handle an event when the event provides data.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

generic<typename TEventArgs>
public delegate void EventHandler(
	Object^ sender, 
	TEventArgs e

Type Parameters


The type of the event data generated by the event.


Type: System::Object

The source of the event.

Type: TEventArgs

An object that contains the event data.

The event model in the .NET Framework is based on having an event delegate that connects an event with its handler. To raise an event, two elements are needed:

  • A delegate that refers to a method that provides the response to the event.

  • Optionally, a class that holds the event data, if the event provides data.

The delegate is a type that defines a signature, that is, the return value type and parameter list types for a method. You can use the delegate type to declare a variable that can refer to any method with the same signature as the delegate.

The standard signature of an event handler delegate defines a method that does not return a value. This method's first parameter is of type Object and refers to the instance that raises the event. Its second parameter is derived from type EventArgs and holds the event data. If the event does not generate event data, the second parameter is simply the value of the EventArgs::Empty field. Otherwise, the second parameter is a type derived from EventArgs and supplies any fields or properties needed to hold the event data.

The EventHandler<TEventArgs> delegate is a predefined delegate that represents an event handler method for an event that generates data. The advantage of using EventHandler<TEventArgs> is that you do not need to code your own custom delegate if your event generates event data. You simply provide the type of the event data object as the generic parameter.

To associate the event with the method that will handle the event, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.

For more information about event handler delegates, see Handling and Raising Events.

The following example shows an event named ThresholdReached. The event is associated with an EventHandler<TEventArgs> delegate.

using namespace System;

public ref class ThresholdReachedEventArgs : public EventArgs
      property int Threshold;
      property DateTime TimeReached;

public ref class Counter
      int threshold;
      int total;

      Counter() {};

      Counter(int passedThreshold)
         threshold = passedThreshold;

      void Add(int x)
          total += x;
          if (total >= threshold) {
             ThresholdReachedEventArgs^ args = gcnew ThresholdReachedEventArgs();
             args->Threshold = threshold;
             args->TimeReached = DateTime::Now;

      event EventHandler<ThresholdReachedEventArgs^>^ ThresholdReached;

      virtual void OnThresholdReached(ThresholdReachedEventArgs^ e)
         ThresholdReached(this, e);

public ref class SampleHandler
      static void c_ThresholdReached(Object^ sender, ThresholdReachedEventArgs^ e)
         Console::WriteLine("The threshold of {0} was reached at {1}.",
                            e->Threshold,  e->TimeReached);

void main()
   Counter^ c = gcnew Counter((gcnew Random())->Next(10));
   c->ThresholdReached += gcnew EventHandler<ThresholdReachedEventArgs^>(SampleHandler::c_ThresholdReached);

   Console::WriteLine("press 'a' key to increase total");
   while (Console::ReadKey(true).KeyChar == 'a') {
      Console::WriteLine("adding one");

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
© 2015 Microsoft