A field-like-event
is a way of declaring both a delegate variable and an event at the same time.
So a field like event (public event EventHandler MyEvent;
) may translate to : ( taken from here)
private EventHandler _myEvent;
public event EventHandler MyEvent
{
add
{
lock (this)
{
_myEvent += value;
}
}
remove
{
lock (this)
{
_myEvent -= value;
}
}
}
Notice private backup field.
However , I was corrected by Jon ( comments section) , that a general-event doen't have a backup field . something like :
public event EventHandler MyEvent
{
add
{
Console.WriteLine ("add operation");
}
remove
{
Console.WriteLine ("remove operation");
}
}
Notice - no backup field.
But then he said that winform act like this :
For instance, in situations where there are lots of events but only a few are likely to be subscribed to, you could have a map from some key describing the event to the delegate currently handling it. This is what Windows Forms does - it means that you can have a huge number of events without wasting a lot of memory with variables which will usually just have null values.
Question :
- How can winform use this map thing to expose events without any backup field ( of delegate type)