Hi, I wrote some code:
public class EventsType
{
public event EventHandler<MyEventArgs<Object>> NewEvent;
public void SmthHappened(string data)
{
MyEventArgs<Object> eventArgs = new MyEventArgs<Object>(data);
OnNewEvent(eventArgs);
}
private void OnNewEvent(MyEventArgs<Object> eventArgs)
{
EventHandler<MyEventArgs<Object>> tempEvent = NewEvent;
if (tempEvent != null)
{
tempEvent(this, eventArgs);
}
}
}
I expected that the C# compiler will translate the NewEvent like this:
private EventHandler<MyEventArgs<object>> _newEvent;
public event EventHandler<MyEventArgs<object>> NewEvent
{
[MethodImpl(MethodImplOptions.Synchronized)]
add
{
_newEvent = (EventHandler<MyEventArgs<object>>)Delegate.Combine(_newEvent, value);
}
[MethodImpl(MethodImplOptions.Synchronized)]
remove
{
_newEvent = (EventHandler<MyEventArgs<object>>)Delegate.Remove(_newEvent, value);
}
}
, but the Reflector says that it is implemented this way:
public event EventHandler<MyEventArgs<object>> NewEvent
{
add
{
EventHandler<MyEventArgs<object>> handler2;
EventHandler<MyEventArgs<object>> newEvent = this.NewEvent;
do
{
handler2 = newEvent;
EventHandler<MyEventArgs<object>> handler3 = (EventHandler<MyEventArgs<object>>) Delegate.Combine(handler2, value);
newEvent = Interlocked.CompareExchange<EventHandler<MyEventArgs<object>>>(ref this.NewEvent, handler3, handler2);
}
while (newEvent != handler2);
}
remove
{
EventHandler<MyEventArgs<object>> handler2;
EventHandler<MyEventArgs<object>> newEvent = this.NewEvent;
do
{
handler2 = newEvent;
EventHandler<MyEventArgs<object>> handler3 = (EventHandler<MyEventArgs<object>>) Delegate.Remove(handler2, value);
newEvent = Interlocked.CompareExchange<EventHandler<MyEventArgs<object>>>(ref this.NewEvent, handler3, handler2);
}
while (newEvent != handler2);
}
}
Please, smb explain me why is it so?