This documentation is archived and is not being maintained.

EventHandler<TEventArgs> Delegate

Represents the method that will handle an event.

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

generic<typename TEventArgs>
where TEventArgs : EventArgs
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 EventArgs 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.

  • A class that holds the event 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, whose first parameter is of type Object and refers to the instance that raises the event, and whose 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 an instance of EventArgs. Otherwise, the second parameter is a custom type derived from EventArgs and supplies any fields or properties needed to hold the event data.

EventHandler<TEventArgs> is a predefined delegate that represents an event handler method for an event, regardless of whether the event generates event data. If your event does not generate event data, substitute EventArgs for the generic type parameter; otherwise, supply your own custom event data type and substitute it for the generic type parameter.

The advantage of using EventHandler<TEventArgs> is that you do not need to code your own custom delegate if your event generates event data. Additionally, the .NET Framework needs only one implementation to support EventHandler<TEventArgs> regardless of the event data type you substitute for the generic type 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 Events and Delegates.

The following code example declares event data and a generic EventHandler<TEventArgs> delegate that uses the event data, and shows how the event is raised.

Notice that the Visual C++ version of the example automatically provides thread-safe access, enabling you to raise the event directly. Therefore, in contrast to the C# and Visual Basic examples, the Visual C++ example code does not require you to create a thread-safe temporary variable.

// This example demonstrates the EventHandler<T> delegate.

using namespace System;
using namespace System::Collections::Generic;

public ref class MyEventArgs: public EventArgs
   String^ msg;

   MyEventArgs( String^ messageData )
   { msg = messageData; }

   property String^ Message 
      String^ get()
      { return msg; }

      void set( String^ value )
      { msg = value; }

public ref class HasEvent
   event EventHandler< MyEventArgs^ >^ SampleEvent;

   // Declare an event of delegate type EventHandler of MyEventArgs.
   void DemoEvent( String^ val )
   //  The CPP compiler automatically provides threadsafe access,
   //  which allows you to raise the event directly:

       SampleEvent(this, gcnew MyEventArgs(val));

public ref class Sample
   static void TestHandler()
      HasEvent^ he = gcnew HasEvent;
      he->SampleEvent += gcnew EventHandler< MyEventArgs^ >( SampleEventHandler );
      he->DemoEvent( L"Hey there, Bruce!" );
      he->DemoEvent( L"How are you today?" );
      he->DemoEvent( L"I'm pretty good." );
      he->DemoEvent( L"Thanks for asking!" );
   static void SampleEventHandler( Object^ src, MyEventArgs^ mea )
      Console::WriteLine( mea->Message );

int main() {

This example produces the following results:

Hey there, Bruce!
How are you today?
I'm pretty good.
Thanks for asking!

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.