DECLARATION OF DELEGATE
class mydelegate
{
public delegate void print(int val);
}
class test
{
public static void display(int val)
{
console.writeline(val);
}
public static void main()
{
mydelegate.print func=new mydelegate.print(10);
func(display);
}
}
EXAMPLE OF DELEGATE BY A SIMPLE PROGRAM
using System;
namespace Deleg
{
class Calculation
{
public delegate void calcHandler(double d1, double d2);
public void calculate(calcHandler clc, double d1,double d2)
{
clc(d1,d2);
}
}
class MyClass
{
public static void add(double d1, double d2)
{
Console.WriteLine("Summation = " + (d1+d2));
}
public static void subtract(double d1, double d2)
{
Console.WriteLine("Subtract = " + (d1-d2));
}
public static void multiply(double d1, double d2)
{
Console.WriteLine("Multiplication = " + (d1*d2));
}
public static void devide(double d1, double d2)
{
Console.WriteLine("Devision = " + (d1/d2));
}
public static void Main()
{
Calculation.calcHandler myDele = new Calculation.calcHandler (add);
myDele += new Calculation.calcHandler(subtract);
myDele += new Calculation.calcHandler(multiply);
myDele += new Calculation.calcHandler(devide);
Calculation c = new Calculation();
c.calculate(myDele,50,40);
Console.Read();
}
}
}
ADVANTAGE OF USING DELEGATE
dynamic binding can be done by using delegate because we can call more than one methods at a time dynamically by calling the delegate in which the methods is defined.
class mydelegate { public delegate void print(int val); } class test { public static void display(int val) { console.writeline(val); } public static void main() { mydelegate.print func=new mydelegate.print(10); func(display); } }
EXAMPLE OF DELEGATE BY A SIMPLE PROGRAM
using System; namespace Deleg { class Calculation { public delegate void calcHandler(double d1, double d2); public void calculate(calcHandler clc, double d1,double d2) { clc(d1,d2); } } class MyClass { public static void add(double d1, double d2) { Console.WriteLine("Summation = " + (d1+d2)); } public static void subtract(double d1, double d2) { Console.WriteLine("Subtract = " + (d1-d2)); } public static void multiply(double d1, double d2) { Console.WriteLine("Multiplication = " + (d1*d2)); } public static void devide(double d1, double d2) { Console.WriteLine("Devision = " + (d1/d2)); } public static void Main() { Calculation.calcHandler myDele = new Calculation.calcHandler (add); myDele += new Calculation.calcHandler(subtract); myDele += new Calculation.calcHandler(multiply); myDele += new Calculation.calcHandler(devide); Calculation c = new Calculation(); c.calculate(myDele,50,40); Console.Read(); } } }ADVANTAGE OF USING DELEGATE
dynamic binding can be done by using delegate because we can call more than one methods at a time dynamically by calling the delegate in which the methods is defined.
0 comments:
Post a Comment