Click here to Skip to main content
15,888,323 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

public delegate MyEventHAndler(object o, EventArgs e);

public class EventGenerator
{
   public event MyEventHandler MyEvent;
   ....
   protected virtual void OnMyEvent(EventArgs e)
   {
     if(MyEvent != null)
     {
        MyEvent(this,e);
     }
   }
}

public class MyTest
{
   EventGenerator eventGen;
   public MyTest()
   {
      eventGen = new EventGenerator();
      eventGen.MyEvent += new MyEventHandler(TestFunction); // What happen here intenally 
   }

   public void TestFunction(object o, EventArgs e)
   {
   }
}


What is happening internally with the following line of code ?
eventGen.MyEvent += new MyEventHandler(TestFunction);


My question is that... is there any data structure to store the reference of the function? what is internally happening? what is += exactly mean ?
Posted
Updated 9-Oct-11 19:32pm
v2

When class will load then it will create a event menthod testFunction.
 
Share this answer
 
Comments
Silju MC 10-Oct-11 1:17am    
Ya thats right...
Silju MC 10-Oct-11 1:19am    
What += mean ? :-)
Event is a special object in .NET which allows you to give notification to the external world. Literally .NET event maps to an existing delegate that acts as a Type of it which eventually points to a method. Thus when the event is subscribed, it needs a method which the object will call at certain time when event is raised in the system. Thus basically an event is a structured way of calling a delegate into a class.

The highlighted EventHandler code is Notifier, which notifies an event or raises an event during the execution of the method CallSubroutine. Hence, if you have subscribed for an event, the event gets raised for each calls to this method.

Once you subscribe to the event, you will get notified for that subscription. You might subscribe as many methods as you want, but each of them should follow the signature of the Handler. So that when, the event is raised, the object will call all the methods subscribed to that event.

Hope, it clears your question.
 
Share this answer
 
Comments
Silju MC 10-Oct-11 1:30am    
Yes its very clear. my 5+

We can subscribe the event by the following line of code.. right ?
eventGen.MyEvent += new MyEventHandler(TestFunction);
My question is that... is there any data structure to store the reference of the function? what is internally happening? what is += exactly mean ?
MyEvent stores a list of subscribers. You add your event handler to that list using +=. When firing, MyEvent calls all event handlers in its list.

You can also detach your event handler when you think it's no longer needed via -=. There is even a scenario when you should do so:
You have an object MyObject which holds an event handler for MyEvent and you want to get rid of MyObject. Since MyEvent still holds a reference to MyObject in its list of event handlers, MyObject will not get garbage collected.
You have to unsubscribe from the event first, then MyObject can be collected.
 
Share this answer
 
Comments
Silju MC 10-Oct-11 7:47am    
Yes. You are right. 5+
To answer this question, I had to do bit of research.

Step 1) I modified your code a bit,

C#
public class EventGenerator
{
    public delegate void MyEventHandler(object o, EventArgs e);
    public event MyEventHandler MyEvent;

    protected virtual void OnMyEvent(EventArgs e)
    {
        if (MyEvent != null)
        {
            MyEvent(this, e);
        }
    }
}
public class MyTest
{
    EventGenerator eventGen;
    public MyTest()
    {
        eventGen = new EventGenerator();
        eventGen.MyEvent += new EventGenerator.MyEventHandler(TestFunction);
        eventGen.MyEvent -= new EventGenerator.MyEventHandler(TestFunction); // newly added
    }

    public void TestFunction(object o, EventArgs e)
    {
    }
}


Compile the code and grab the exe and drop into the ILDasm[^]program.

Step 2) From the ILDasm program, we can see,
C#
eventGen.MyEvent += new EventGenerator.MyEventHandler(TestFunction);

has been translated as
IL_0025:  callvirt   instance void ConsoleApplication18.EventGenerator::add_MyEvent(class ConsoleApplication18.EventGenerator/MyEventHandler)

C#
eventGen.MyEvent -= new EventGenerator.MyEventHandler(TestFunction);

has been translated as,
IL_003d:  callvirt   instance void ConsoleApplication18.EventGenerator::remove_MyEvent(class ConsoleApplication18.EventGenerator/MyEventHandler)

Step 3) Now the Question where is the add_MyEvent and remove_MyEvent comes from, the answer could be find out from two places,
1. CLR Via C# by Jeffrey Richter Page 270
2..Net Reflector, if we put that compiled exe or dll (which contains the above code) then following code will be find out from MyEvent class,

add_MyEvent,

public void add_MyEvent(MyEventHandler value)
{
    MyEventHandler handler2;
    MyEventHandler myEvent = this.MyEvent;
    do
    {
        handler2 = myEvent;
        MyEventHandler handler3 = (MyEventHandler) Delegate.Combine(handler2, value);
        myEvent = Interlocked.CompareExchange<MyEventHandler>(ref this.MyEvent, handler3, handler2);
    }
    while (myEvent != handler2);
}



and remove_MyEvent

public void remove_MyEvent(MyEventHandler value)
{
    MyEventHandler handler2;
    MyEventHandler myEvent = this.MyEvent;
    do
    {
        handler2 = myEvent;
        MyEventHandler handler3 = (MyEventHandler) Delegate.Remove(handler2, value);
        myEvent = Interlocked.CompareExchange<MyEventHandler>(ref this.MyEvent, handler3, handler2);
    }
    while (myEvent != handler2);
}


Hope it answer your questions :)
 
Share this answer
 
Comments
Silju MC 10-Oct-11 7:46am    
yes. Thanks a lot :-) my 5+ too...
Mohammad A Rahman 10-Oct-11 7:49am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900