Sunday, January 20, 2013

Csharp / C# Multicast Delegate example


using System;
 
//observation: A delegate which takes two paramter of integer type.
public delegate void fnTwoInteger(int x,int y);
 
public class DelegateExample
{
    public static void Main()
    {
        ExmapleClass obj = new ExmapleClass();
 
        fnTwoInteger pointer=null;
 
        //observation: adding more than one function called multicast delegate.
        pointer += new fnTwoInteger(obj.Add);
 
        pointer += new fnTwoInteger(obj.Multiply);
 
        pointer(52, 72);
 
        Console.ReadLine();
    }
}
 
public class ExmapleClass
{
    public void Add(int x, int y)
    {
        Console.WriteLine("The sum is: " + (x + y));
    }
 
    public void Multiply(int x, int y)
    {
        Console.WriteLine("The product is: " + (x * y));
    }
}
Output:
The sum is: 124
The product is: 3744

No comments:

Post a Comment