Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Consider the example below:
C#
private void button1_Click(object sender, EventArgs e)
   {
     DelegateSample obj = new DelegateSample();
     DelFunc delobj1 = new DelFunc(obj.func1);
     delobj1 += new DelFunc(obj.func2);
     delobj1 += new DelFunc(obj.func3);
     obj.functionAll(delobj1);
   }
 }
 class DelegateSample
 {
   public void func1()
   {
     MessageBox.Show("You just called func1");
   }
   public void func2()
   {
     MessageBox.Show("You just called func2");
   }
   public void func3()
   {
     MessageBox.Show("You just called func3");
   }
   public void functionAll(Delegate o)
   {
   Delegate[] objdel= o.GetInvocationList();
   for (int i = 0; i < objdel.Length; i++)
   {
     DelFunc obj = (DelFunc)objdel[i];
     obj.Invoke();
   }
   }

Here i have a delegate object which references 3 methods.I have added the method references and the delegate object is being passed as an argument to a method.This method is called when the buttom is clicked.Hence I'd like to know how these methods are called during "RUNTIME" when I have already added the references of all the 3 methods in the compile time.
Posted
Updated 27-Jul-11 4:52am
v2
Comments
bbirajdar 3-Feb-12 7:55am    
You need to read the chapter on delegates from Wrox Professional C#

Well, your code is convoluted. Why are you dong this ? What is it you want to know ? Does your code work and you want to know HOW it works ? The methods get called BECAUSE you've got references to them, so it knows what to call.
 
Share this answer
 
 
Share this answer
 
Comments
Monjurul Habib 27-Jul-11 18:43pm    
nice links, my 5
Uday P.Singh 28-Jul-11 1:49am    
thanks Monjurul :)
This is a multicast delegate[^].
The calling order is synchronous to the extent they are called in the same order as they are added in.
 
Share this answer
 
v2
Comments
Monjurul Habib 27-Jul-11 18:42pm    
nice one,my 5.
The delegate object hold a reference of a method.The method will then can be called by the delegate object.
See:Delegates example
 
Share this answer
 

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