Delegates and Lambdas
Passing Delegates
A helper method can receive a delegate and call it.
callback
A callback is callable behavior passed into another method.
Passing Delegates
PassingDelegates.cs
Replay: real traced execution (multi-file project)
using System;
class Program
{
static int Apply(int value, Func<int, int> transform)
{
return transform(value);
}
static void Main()
{
int factor = 3;
Func<int, int> multiply = value => value * factor;
int result = Apply(5, multiply);
Console.WriteLine($"factor={factor}");
Console.WriteLine($"result={result}");
}
}
using System;
class Program
{
static int Apply(int value, Func<int, int> transform)
{
return transform(value);
}
static void Main()
{
int factor = 2;
Func<int, int> multiply = value => value * factor;
int result = Apply(5, multiply);
Console.WriteLine($"factor={factor}");
Console.WriteLine($"result={result}");
}
}
using System;
class Program
{
static int Apply(int value, Func<int, int> transform)
{
return transform(value);
}
static void Main()
{
int factor = 4;
Func<int, int> multiply = value => value * factor;
int result = Apply(5, multiply);
Console.WriteLine($"factor={factor}");
Console.WriteLine($"result={result}");
}
}
factor ← 3, multiply ← System.Func`2[System.Int32,System.Int32]
10static void Main()11{12 int factor→ 3 = 3; //@factor=2, 413 Func<int, int> multiply→ System.Func`2[System.Int32,System.Int32] = value => value * factor;14 int result = Apply(5, multiplySystem.Func`2[System.Int32,System.Int32]);static int Apply(int value, Func<int, int> transform)
4{5 static int Apply(int value5, Func<int, int> transformSystem.Func`2[System.Int32,System.Int32])6 {7 return transform(value5);8 }result ← 15
13 Func<int, int> multiply = value => value * factor;14 int result→ 15 = Apply(5, multiplySystem.Func`2[System.Int32,System.Int32]);1516 Console.WriteLine($"factor={factor3}");17 Console.WriteLine($"result={result15}");18}outputfactor=3 result=15
factor ← 2, multiply ← System.Func`2[System.Int32,System.Int32]
10static void Main()11{12 int factor→ 2 = 2;13 Func<int, int> multiply→ System.Func`2[System.Int32,System.Int32] = value => value * factor;14 int result = Apply(5, multiplySystem.Func`2[System.Int32,System.Int32]);static int Apply(int value, Func<int, int> transform)
4{5 static int Apply(int value5, Func<int, int> transformSystem.Func`2[System.Int32,System.Int32])6 {7 return transform(value5);8 }result ← 10
13 Func<int, int> multiply = value => value * factor;14 int result→ 10 = Apply(5, multiplySystem.Func`2[System.Int32,System.Int32]);1516 Console.WriteLine($"factor={factor2}");17 Console.WriteLine($"result={result10}");18}outputfactor=2 result=10
factor ← 4, multiply ← System.Func`2[System.Int32,System.Int32]
10static void Main()11{12 int factor→ 4 = 4;13 Func<int, int> multiply→ System.Func`2[System.Int32,System.Int32] = value => value * factor;14 int result = Apply(5, multiplySystem.Func`2[System.Int32,System.Int32]);static int Apply(int value, Func<int, int> transform)
4{5 static int Apply(int value5, Func<int, int> transformSystem.Func`2[System.Int32,System.Int32])6 {7 return transform(value5);8 }result ← 20
13 Func<int, int> multiply = value => value * factor;14 int result→ 20 = Apply(5, multiplySystem.Func`2[System.Int32,System.Int32]);1516 Console.WriteLine($"factor={factor4}");17 Console.WriteLine($"result={result20}");18}outputfactor=4 result=20