0
0
CsharpHow-ToBeginner · 3 min read

How to Create Custom Event Args in C# Easily

To create custom event arguments in C#, define a class that inherits from EventArgs and add your custom properties. Then use this class as the type for the event handler's second parameter to pass extra data when the event is raised.
📐

Syntax

To create custom event arguments, you define a class that inherits from EventArgs. This class holds any extra data you want to send with the event. Then, declare an event using EventHandler<YourCustomEventArgs> to use your custom arguments.

  • CustomEventArgs: Your class with extra data.
  • EventHandler<T>: Generic delegate for events with custom args.
  • sender: The source of the event.
  • e: The event arguments object.
csharp
public class CustomEventArgs : EventArgs
{
    public string Message { get; }

    public CustomEventArgs(string message)
    {
        Message = message;
    }
}

public event EventHandler<CustomEventArgs> CustomEvent;
💻

Example

This example shows how to create a custom event args class, raise an event with it, and handle the event to access the custom data.

csharp
using System;

public class CustomEventArgs : EventArgs
{
    public string Message { get; }

    public CustomEventArgs(string message)
    {
        Message = message;
    }
}

public class Publisher
{
    public event EventHandler<CustomEventArgs> RaiseCustomEvent;

    public void DoSomething()
    {
        OnRaiseCustomEvent(new CustomEventArgs("Hello from custom event args!"));
    }

    protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
    {
        RaiseCustomEvent?.Invoke(this, e);
    }
}

public class Subscriber
{
    public void Subscribe(Publisher pub)
    {
        pub.RaiseCustomEvent += HandleCustomEvent;
    }

    private void HandleCustomEvent(object sender, CustomEventArgs e)
    {
        Console.WriteLine($"Event received with message: {e.Message}");
    }
}

class Program
{
    static void Main()
    {
        Publisher pub = new Publisher();
        Subscriber sub = new Subscriber();
        sub.Subscribe(pub);

        pub.DoSomething();
    }
}
Output
Event received with message: Hello from custom event args!
⚠️

Common Pitfalls

Some common mistakes when creating custom event args include:

  • Not inheriting from EventArgs, which breaks convention and can confuse users.
  • Using EventHandler instead of EventHandler<T> for custom args, losing type safety.
  • Not checking if the event is null before invoking it, which can cause exceptions.
csharp
/* Wrong: Not inheriting from EventArgs */
public class WrongArgs
{
    public int Data { get; set; }
}

/* Wrong: Using EventHandler without generic type */
public event EventHandler WrongEvent;

/* Right: Inherit from EventArgs and use EventHandler<T> */
public class RightArgs : EventArgs
{
    public int Data { get; set; }
}
public event EventHandler<RightArgs> RightEvent;

/* Right: Check for null before invoking */
protected virtual void OnRightEvent(RightArgs e)
{
    RightEvent?.Invoke(this, e);
}
📊

Quick Reference

  • Inherit your custom event args class from EventArgs.
  • Use EventHandler<YourCustomEventArgs> for your event declaration.
  • Raise events by invoking the event delegate with sender and your custom args.
  • Always check if the event is not null before invoking.

Key Takeaways

Create a class inheriting from EventArgs to hold custom event data.
Use EventHandler with your custom args class for type-safe events.
Always check if the event is null before invoking to avoid errors.
Pass the sender and custom event args when raising the event.
Subscribe to the event with a method matching the EventHandler signature.