- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How does the event pattern work in .NET?
Events are a simplified pattern that uses delegates. In C#, all delegates have the multicast capability, i.e., an instance of a delegate can represent not just a single method but a list of methods. For example −
Example
delegate int Transformer(int x); static void Main(){ Transformer perform = x =>{ int result = x * x; Console.WriteLine(result); return result; }; perform += x =>{ int result = x * x * x; Console.WriteLine(result); return result; }; perform(2); // prints 4 and 8 }
Calling Perform() calls both the methods that were added to the delegate. The methods are invoked in the order in which they were added.
This allows you to implement the publish-subscribe, also known as the event pattern. The publisher contains a delegate field. It decides when to publish by invoking the delegate. A subscriber adds its own methods to the publisher's delegate, so they get notified whenever the publisher decides to publish by invoking the delegate. A subscriber doesn't know about or interfere with other subscribers.
C# events are a language feature that formalizes the above pattern in a type-safe way that prevents common mistakes with the delegates. An event exposes a subset of delegate features needed to implement the event pattern. The following example illustrates this.
First, define the structure of the data that you want to send when the event happens.
public class ThresholdReachedEventArgs : EventArgs{ public int Threshold { get; set; } public DateTime TimeReached { get; set; } }
Then, define the Publisher, i.e. the class that wants to raise an event.
public class Counter{ // 1. Define an event public event EventHandler<ThresholdReachedEventArgs> ThresholdReached; // 2. Do something that raises the event, and pass the EventArgs custom data public void DoSomething(){ ThresholdReachedEventArgs e = new ThresholdReachedEventArgs{ Threshold = 10, TimeReached = DateTime.Now }; // 3. Raise the actual event ThresholdReached?.Invoke(this, e); } }
Finally, create one or many subscribers. Classes that want to listen to an event raised by the Publisher.
class Program{ static void Main(string[] args){ // 1. Create an instance of the publisher, which you want to listen to Counter counter = new Counter(); // 2. Attach an event handler on the publisher counter.ThresholdReached += OnThresholdReached; // 3. Do something that will raise the event. Now you are ready to listen to the event. counter.DoSomething(); } // 4. Handle the event which is raised by publisher static void OnThresholdReached(object sender, ThresholdReachedEventArgs e){ Console.WriteLine($"Reached Threshold {e.Threshold} at {e.TimeReached.ToString()}"); } }
Example
using System; class Program{ static void Main(string[] args){ // 1. Create an instance of the publisher, which you want to listen to Counter counter = new Counter(); // 2. Attach an event handler on the publisher counter.ThresholdReached += OnThresholdReached; // 3. Do something that will raise the event. Now you are ready to listen to the event. counter.DoSomething(); } // 4. Handle the event which is raised by publisher static void OnThresholdReached(object sender, ThresholdReachedEventArgs e){ Console.WriteLine($"Reached Threshold {e.Threshold} at {e.TimeReached.ToString()}"); } } public class ThresholdReachedEventArgs : EventArgs{ public int Threshold { get; set; } public DateTime TimeReached { get; set; } } public class Counter{ // 1. Define an event public event EventHandler<ThresholdReachedEventArgs> ThresholdReached; // 2. Do something that raises the event, and pass the EventArgs custom data public void DoSomething(){ ThresholdReachedEventArgs e = new ThresholdReachedEventArgs{ Threshold = 10, TimeReached = DateTime.Now }; // 3. Raise the actual event ThresholdReached?.Invoke(this, e); } }
Output
Reached Threshold 10 at 5/15/2021 12:49:10 PM
- Related Articles
- How does jQuery Datepicker onchange event work?
- What is Kicker Pattern and how does It work?
- Explain the iterator pattern in .NET
- Explain how the assemblies and DLLs work in .NET
- How to work with XML and JSON in .NET?
- How does the magnets work?
- How does the array_diff_key() work in PHP?
- How does the Microstrip antenna work?
- How does the Selenium WebDriver work?
- How does the discordancy testing work?
- How does the WannaCry malware work?
- How does the Docker network work?
- How does jQuery.scrollTop() work?
- How does jQuery.scrollLeft() work?
- How does classification work?
