c# delegates 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: what are delegates and how to use them c#
public delegate void MyDelegate(string text);
Example 3: delegate declaration in c#
public delegate int PerformCalculation(int x, int y);
Example 4: what are delegates and how to use them c#
MyDelegate d = new MyDelegate(ShowText);