Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a simple implementation of a class that exposes an event.</p> <pre><code>public class ChangeNotifier { // Local data private int num; // Ctor to assign data public ChangeNotifier(int number) { this.num = number; } // The event that can be subscribed to public event EventHandler NumberChanged; public int Number { get { return this.num; } set { // If the value has changed... if (this.num != value) { // Assign the new value to private storage this.num = value; // And raise the event if (this.NumberChanged != null) this.NumberChanged(this, EventArgs.Empty); } } } } </code></pre> <p>This class may be used something like as follows:</p> <pre><code>public void SomeMethod() { ChangeNotifier notifier = new ChangeNotifier(10); // Subscribe to the event and output the number when it fires. notifier.NumberChanged += (s, e) =&gt; Console.Writeline(notifier.Number.ToString()); notifier.Number = 10; // Does nothing, this is the same value notifier.Number = 20; // Outputs "20" because the event is raised and the lambda runs. } </code></pre> <p>Regarding control flow, execution flows into <code>SomeMethod()</code>. We create a new <code>ChangeNotifier</code> and thus call its constructor. This assigns the value of <code>10</code> to the private <code>num</code> member.</p> <p>We then subscribe to the event using the <code>+=</code> syntax. This operator takes a delegate on the right hand side (in our case, that delegate is a lambda) and adds it to the collection of delegates on the event. This operation doesn't execute any code that we've written in the <code>ChangeNotifier</code>. It can be customized through the <code>add</code> and <code>remove</code> methods on the event if you'd like, but there's rarely a need to do that.</p> <p>Then we perform a couple simple operations on the <code>Number</code> property. First we assign <code>10</code>, which runs the <code>set</code> method on the <code>Number</code> property with <code>value = 10</code>. But the <code>num</code> member is already valued at <code>10</code>, so the initial conditional evaluates to false and nothing happens.</p> <p>Then we do the same thing with <code>20</code>. This time the value is different, so we assign the new value to <code>num</code> and fire the event. First we verify that the event is not null. It's null if nothing has subscribed to it. If it's not null (ie, if something <em>is</em> subscribed to it), we fire it using the standard method/delegate syntax. we simply call the event with the event's arguments. This will call all methods that have subscribed to the event, including our lambda that will perform a <code>Console.WriteLine()</code>.</p> <hr> <p>Henrik has successfully nitpicked the potential race condition that exists if one thread can be in <code>Number</code>'s setter while another thread is unsubscribing a listener. I don't consider that a common case for someone who doesn't yet understand how events work, but if you're concerned about that possibility, modify these lines:</p> <pre><code>if (this.NumberChanged != null) this.NumberChanged(this, EventArgs.Empty); </code></pre> <p>to be something like this:</p> <pre><code>var tmp = this.NumberChanged; if (tmp != null) tmp(this, EventArgs.Empty); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload