C# Language Events Anonymous Event Handler Declaration

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Event declaration:

public event EventHandler<EventArgsType> EventName;

Event handler declaration using lambda operator => and subscribing to the event:

EventName += (obj, eventArgs) => { /* Handler logic */ };

Event handler declaration using delegate anonymous method syntax:

EventName += delegate(object obj, EventArgsType eventArgs) { /* Handler Logic */ };

Declaration & subscription of an event handler that does not use the event's parameter, and so can use the above syntax without needing to specify parameters:

EventName += delegate { /* Handler Logic */ }

Invoking the event:

EventName?.Invoke(SenderObject, EventArguments);


Got any C# Language Question?