delegate declaration in c# code example
Example 1: 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();
}
}
}
Example 2: delegate function c#
public delegate void Del(string message);
public static void DelegateMethod(string message)
{
Console.WriteLine(message);
}
Del hadler = DelegateMethod;
hadler("Hello World");
Example 3: delegate declaration in c#
public delegate int PerformCalculation(int x, int y);