Lambda Expression ใน C# อธิบาย Pattern มี Token => เรียกว่า "goes to" Operator
ตัวอย่าง
using System;
using System.Collections.Generic;
class Demo
{
static void Main()
{
List<int> list = new List<int>() { 21, 17, 40, 11, 9 };
int res = list.FindIndex(x => x % 2 == 0); // หา Index ของเลขคู่แรก
Console.WriteLine("Index: " + res);
}
}
Output
Index: 2
คำอธิบาย: x => x % 2 == 0 คือ Lambda Expression หา Index ของ Element แรกที่หาร 2 ลงตัว (40 อยู่ Index 2)