Delegate
using System;
delegate int CalculatorDelegate(int num1, int num2);
class Program {
static void Main(string[] args) {
// Initialize
CalculatorDelegate calculatorDelegate = new CalculatorDelegate(MaxNumber);
// Call a method through delegate
int result = calculatorDelegate(10, 20);
// Print result
Console.WriteLine("Max number: " + result);
}
static int MaxNumber(int num1, int num2) {
return (num1 > num2) ? num1 : num2;
}
}Use delegates to call different functions
Use the delegate to create a callback
Use delegates in Lambda Expression
Last updated