1

event keyword creates two methods add_* and remove_* (its working like property).

what is the difference

public event MyDelegate event1;
public MyDelegate event2 { get; set; }

why should use event keyowrd?

ika_919
  • 11
  • 4
  • to prevent `myItem.event2 = null;`, `myItem.event2 = myOther.event2;` etc. with `event` all we can do is to *add* and *remove* but not *call*, *assign* etc. dangerous operations – Dmitry Bychenko Jan 20 '17 at 13:59

1 Answers1

1

One reason is safety. If you implement an event like delegate field:

 public MyDelegate event2 { get; set; }

you'll be able to do dangerous things:

 MyObject test = new MyObject();

 // removing all listeners
 test.event2 = null; 
 // just a little typo `=` instead of `+=`
 // means now complete substitution of the listeners instead of adding one  
 test.event2 = myMethod; 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215