Example of Event in C# - Delegate with the custom EventArgs
☰Fullscreen
Table of Content:
Step-by-Step
-
Define a custom
EventArgsclass:In this context,
NewIssueEventArgsis a derived class ofEventArgs. This means thatNewIssueEventArgsinherits fromEventArgs, making it a specialized version ofEventArgswith additional properties specific to the new issue event (e.g.,IssueNumberandIssueTitle).Here's a breakdown:
- Base Class:
EventArgsis a predefined class in the .NET framework used as the base class for classes containing event data. - Derived Class:
NewIssueEventArgsinherits fromEventArgsand adds additional properties (IssueNumberandIssueTitle).
- Base Class:
-
The event to use the custom
EventArgsclass: - The event handler to use the custom
EventArgsclass: - Example usage:
public class NewIssueEventArgs : EventArgs { public int IssueNumber { get; } public string IssueTitle { get; } public NewIssueEventArgs(int issueNumber, string issueTitle) { IssueNumber = issueNumber; IssueTitle = issueTitle; } }
public class MagazineCompany { // Define a delegate with the custom EventArgs public delegate void NewIssueEventHandler(object sender, NewIssueEventArgs e); // Define an event based on the delegate public event NewIssueEventHandler NewIssue; // Method to raise the event with custom event data public void PublishNewIssue(int issueNumber, string issueTitle) { Console.WriteLine("Magazine Company: Publishing new issue..."); OnNewIssue(new NewIssueEventArgs(issueNumber, issueTitle)); } // Protected virtual method to allow derived classes to override the event invocation behavior protected virtual void OnNewIssue(NewIssueEventArgs e) { // Invoke the event handler, if there are any subscribers NewIssue?.Invoke(this, e); } }
public class Reader { // Method that will be called when the event is raised public void OnNewIssueReceived(object sender, NewIssueEventArgs e) { Console.WriteLine($"Reader: Received new issue {e.IssueNumber} titled '{e.IssueTitle}'."); } }
public class Program { public static void Main(string[] args) { // Create a magazine company (Publisher) MagazineCompany magazineCompany = new MagazineCompany(); // Create a reader (Subscriber) Reader reader = new Reader(); // Subscribe the reader to the magazine company's new issue event magazineCompany.NewIssue += reader.OnNewIssueReceived; // Publish a new issue with additional event data magazineCompany.PublishNewIssue(42, "The Future of Technology"); } }
Explanation
- Custom
EventArgsClass:NewIssueEventArgscontains properties for the issue number and title. - Delegate and Event:
NewIssueEventHandleruses the customEventArgs. - Raising the Event:
OnNewIssuemethod raises the event, passing an instance ofNewIssueEventArgswith the issue number and title. - Event Handler:
OnNewIssueReceivedmethod ofReaderclass handles the event and can access the additional data (issue number and title).
When you run the program, the output will be:
Magazine Company: Publishing new issue... Reader: Received new issue 42 titled 'The Future of Technology'.