example of delegate in c#
Example 1: c# delegate
public delegate double MyDelegateType(double x);
public double Consummer(double x, MyDelegateType f){
return f(x);
}
public double MyFunctionMethod(double x){
return x;
}
public void Client(){
double result = Consummer(1.234, x => x * 456.1234);
double secondResult = Consummer(2.345, MyFunctionMethod);
}
Example 2: C# delegate
using System;
public class CargoAircraft
{
public delegate void CheckQuantity();
public CheckQuantity ProcessQuantity;
public void ProcessRequirements()
{
ProcessQuantity();
}
}
public class CargoCounter
{
public void CountQuantity() { }
}
class Program
{
static void Main(string[] args)
{
CargoAircraft cargo = new CargoAircraft();
CargoCounter cargoCounter = new CargoCounter();
cargo.ProcessQuantity += cargoCounter.CountQuantity;
cargo.ProcessRequirements();
}
}
}