C# understanding delegate syntax code example
Example 1: c# delegate
// Declare a delegate type
public delegate double MyDelegateType(double x);
// Define some consummer that uses functions of the delegate type
public double Consummer(double x, MyDelegateType f){
return f(x);
}
//...
// Define a function meant to be passed to the consummer
// The only requirement is that it matches the signature of the delegate
public double MyFunctionMethod(double x){
// Can add more complicated logic here
return x;
}
// In practice now
public void Client(){
double result = Consummer(1.234, x => x * 456.1234);
double secondResult = Consummer(2.345, MyFunctionMethod);
}
Example 2: delegate declaration in c#
public delegate int PerformCalculation(int x, int y);