Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.EventArgs — event data

System.EventArgs is the base class used in the standard .NET event pattern. A custom derived class carries data to handlers, while EventArgs.Empty is used when raising events without additional data.

Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification

Syntax

public class EventArgs
public class MessageEventArgs : EventArgs
public event EventHandler<MessageEventArgs>? MessageReceived;

System.EventArgs is a .NET library class, not a C# keyword. In the standard event pattern, it is the base for classes that hold event data. It defines no application-data properties itself; its public EventArgs() constructor creates an empty instance. It is available in .NET 10 and is not new to that version.

To pass data, define a derived class with a name ending in EventArgs, such as MessageEventArgs, and add the necessary properties. An EventHandler<MessageEventArgs> event passes two arguments to its handler: sender, the event source, and e, the data. Deriving from EventArgs is a convention of this pattern, not a general requirement for every C# event.

If an event carries no additional data, use EventHandler and the EventArgs.Empty field instead of null or creating another empty object. EventArgs describes data, whereas the EventHandler delegate describes the handler signature — these are different roles.

In the example, MessageReceived passes text through a read-only property, and Completed uses EventArgs.Empty. this identifies the source of both events; ?.Invoke skips invocation when there are no subscribers. The expected output is Hello followed by Completed.

Examples

#nullable enable
using System;

var source = new MessageSource();
source.MessageReceived += (sender, e) => Console.WriteLine(e.Message);
source.Completed += (sender, e) => Console.WriteLine("Completed");
source.Run();

public sealed class MessageEventArgs : EventArgs
{
    public MessageEventArgs(string message) => Message = message;
    public string Message { get; }
}

public sealed class MessageSource
{
    public event EventHandler<MessageEventArgs>? MessageReceived;
    public event EventHandler? Completed;

    public void Run()
    {
        MessageReceived?.Invoke(this, new MessageEventArgs("Hello"));
        Completed?.Invoke(this, EventArgs.Empty);
    }
}

Result

Hello
Completed

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources