static void TraditionalDelegateSyntax()
{
List<int> list = new List<int>();
list.AddRange(new int[] { 20, 1, 4, 8, 9, 44 });
Predicate<int> callback = IsEvenNumber;
List<int> evenNumbers = list.FindAll(callback);
Console.WriteLine("Here are your even numbers:");
foreach (int evenNumber in evenNumbers) { Console.Write($"{evenNumber}\t"); }
Console.WriteLine();
}
static void AnonymousMethodSyntax()
{
List<int> list = new List<int>();
list.AddRange(new int[] { 20, 1, 4, 8, 9, 44 });
List<int> evenNumbers = list.FindAll(delegate (int i) { return (i & 2) == 0; });
foreach (int evenNumber in evenNumbers)
{
Console.WriteLine(evenNumber);
}
}
static void LambdaExpressionSyntax()
{
List<int> list = new List<int>();
list.AddRange(new int[] { 20, 1, 4, 8, 9, 44 });
List<int> evenNumbers = list.FindAll((i) =>
{
Console.WriteLine($"value of i is currently: {i}");
bool isEven = ((i & 2) == 0);
return isEven;
});
Console.WriteLine("Here are your even numbers:");
foreach(int evenNumber in evenNumbers)
{
Console.WriteLine($"{evenNumber}\t");
}
Console.WriteLine();
}
static bool IsEvenNumber(int i) { return i % 2 == 0; }
В методе Main вызвал все три метода, но только первый дал верный результат(20, 4, 8, 44). Остальные два вернули список без изменений.