I want to make a list that stores some events and attach event handler to the event through the list.
So I make a List<dele>
add the event anEvent
into it, then I try to attach an event handler to that event, but at last anEvent
still got no event handler attached to it, the program outputs True. But the delegate stored at list[0]
did get the lambda expression.
public delegate void dele();
class Program
{
static event dele anEvent;
static void Main(string[] args)
{
List<dele> list=new List<dele>();
list.Add(anEvent);
list[0]+=()=>{Console.WriteLine("BEEP!");};
Console.WriteLine(anEvent==null);
}
}
Isn't delegate a reference type? It seems that eventhandler
and list[0]
refers to different objects. I wonder why.
If I want that anEvent
gets the event handler when I attach the handler to list[0]
, what should I do?
Thanks!