Edit

Share via


EventHandler<TEventArgs> Delegate

Definition

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

C#
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
C#
public delegate void EventHandler<TEventArgs>(object? sender, TEventArgs e);
C#
[System.Serializable]
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e) where TEventArgs : EventArgs;
C#
[System.Serializable]
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);

Type Parameters

TEventArgs

The type of the event data generated by the event.

Parameters

sender
Object

The source of the event.

e
TEventArgs

An object that contains the event data.

Attributes

Examples

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

C#
using System;

namespace ConsoleApplication3
{
    public class Program3
    {
        public static void Main()
        {
            Counter c = new(new Random().Next(10));
            c.ThresholdReached += c_ThresholdReached;

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

        static void c_ThresholdReached(object? sender, ThresholdReachedEventArgs e)
        {
            Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold,  e.TimeReached);
            Environment.Exit(0);
        }
    }

    class Counter
    {
        private readonly int _threshold;
        private int _total;

        public Counter(int passedThreshold)
        {
            _threshold = passedThreshold;
        }

        public void Add(int x)
        {
            _total += x;
            if (_total >= _threshold)
            {
                ThresholdReachedEventArgs args = new()
                {
                    Threshold = _threshold,
                    TimeReached = DateTime.Now
                };
                OnThresholdReached(args);
            }
        }

        protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
        {
            ThresholdReached?.Invoke(this, e);
        }

        public event EventHandler<ThresholdReachedEventArgs>? ThresholdReached;
    }

    public class ThresholdReachedEventArgs : EventArgs
    {
        public int Threshold { get; set; }
        public DateTime TimeReached { get; set; }
    }
}

Remarks

The event model in .NET 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 don't 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.

Extension Methods

GetMethodInfo(Delegate)

Gets an object that represents the method represented by the specified delegate.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

See also