Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The event keyword creates an accessor for a private delegate object. The exact same thing a property does, it restricts access to a private field. Your code snippet fails with a similar kind of error when you use a property instead of an event:</p> <pre><code> class ClassA { public int Property { get; set; } } class ClassB { public ClassB() { ClassA a = new ClassA(); ClassC c = new ClassC(); c.setValue(ref a.Property); // CS0206 } } class ClassC { public void setValue(ref int value) { value = 42; } } </code></pre> <p>It is easier to see now, there is no way for the compiler to ensure that the setValue() method uses the property setter. Nor could it know that the "value" argument is a property with a setter or a plain field.</p> <p>It is less clear for an event because there is so much syntax sugar at work. This declaration</p> <pre><code>public event EventHandler SomeEvent; </code></pre> <p>actually generates this code:</p> <pre><code>private EventHandler _SomeEvent; public event SomeEvent { add { _SomeEvent += new EventHandler(value); } remove { _SomeEvent -= new EventHandler(value); } } </code></pre> <p>The add and remove accessors are equivalent to the get and set accessors of a property, they prevent code from messing with the private _SomeEvent field. By convention, the add accessor is invoked when you use +=, remove is invoked with -=. Compare this with the earlier example I gave for a property. Same problem, you can't use the ref keyword and ClassC.addListener() would have no way to know that the handler is actually an event instead of a delegate object. If the compiler would pass _SomeEvent instead, the point of using the accessors is lost.</p> <p>You can restructure the code to solve this problem:</p> <pre><code> class ClassC { public EventHandler getListener() { return new EventHandler(onEvent); } private void onEvent(object sender, EventArgs e) { } } ... a.SomeEvent += c.getListener(); </code></pre> <hr> <p>One final note: the symmetry between an event and a property is a bit lost, the C# compiler automatically generates the add/remove accessors if you don't write them explicitly. It doesn't do this for a property. It would have made automatic properties a lot easier:</p> <pre><code>property int Property; </code></pre> <p>But that would have required adding a new keyword to the language, something the C# team really dislikes. Other languages like VB.NET and C++/CLI do have that keyword.</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