Calling functions through Delegates

Description



Here is the code snippet demonstrate that how to call multiple member functions through Multicast Delegates.


class A
{
//Declared the Multicast Delegate(Void Return type)
public delegate void MyDelegate(int a,int b);
public static void Main(string[] args)
{
B b = new B();
MyDelegate Handler;
Handler = new MyDelegate(b.Add); //Added address of first function.
Handler += new MyDelegate(b.Subtract); //Added address of first function.

Handler(3, 4); //Calling functions.
Console.ReadLine();
}

}
class B
{
public void Add(int a, int b)
{
Console.WriteLine("Sum is:{0}",a + b);
}
public void Subtract(int a, int b)
{
Console.WriteLine("Difference is:{0}", Math.Abs(a - b));
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: