Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Because when you define an <a href="http://msdn.microsoft.com/en-us/library/8627sbea%28v=vs.110%29.aspx" rel="nofollow"><code>event</code></a>, with the <code>event SomeHandler SomeEvent</code> syntax, it default-defines <a href="http://msdn.microsoft.com/en-us/library/cc713648.aspx" rel="nofollow"><code>add</code> and <code>remove</code></a> for you. The point here is that the <em>only</em> way you publicly interact with an <code>event</code> is through <code>add</code> and <code>remove</code> (accessed by <code>+=</code> and <code>-=</code>, respectively). The language does not allow a "reset all" action, which is why <code>=</code> is not allowed.</p> <p>To address the more underlying question of a "design choice" for this...I suppose the driving force is that the <em>contract</em> of an <code>event</code> -- a multicast model where subscribers are free to add and remove their subscriptions. Allowing anyone to unsubscribe all subscribers would go against that contract and allowing only one subscriber <em>isn't</em> the contract.</p> <p><strong>Addendum:</strong> It looks like what you're looking for is the ability to expose the delegate directly, which can be done if you just rely on .NET's multicast delegate syntax. It looks a lot like the <code>event</code> syntax, but allows for clearing with <code>=</code> and is even smart enough to understand <code>+=</code> on a <code>null</code> <code>delegate</code> (see <a href="http://ideone.com/xdBG6" rel="nofollow">output</a>):</p> <pre><code>public delegate void F(int x); public static void Main() { F f = null; f += x =&gt; System.Console.WriteLine("First: {0}", x); f += x =&gt; System.Console.WriteLine("Second: {0}", x); f(5); } </code></pre> <p>Alternatively, you could use the long form of <code>event</code> with your <code>add</code>/<code>remove</code> hitting an internal list and provide a method that allows for clearing that list. </p>
 

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