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?
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?
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;