C# 中的 lambda 表示式是什麼?


C# 中的 lambda 表示式描述了一個模式。它在表示式環境中具有 token =>。當宣告 lambda 表示式時,這可解釋為“轉到”運算子。

以下示例說明了如何在 C# 中使用 lambda 表示式 −

示例

 即時演示

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);
      Console.WriteLine("Index: "+res);
   }
}

輸出

Index: 2

上面,我們看到了使用“轉到”運算子查詢偶數索引 −

list.FindIndex(x => x % 2 == 0);

以上示例給出了以下輸出。

Index: 2

偶數位於索引 2,即它是第 3 個元素。

更新於: 20-Jun-2020

206 次瀏覽

開啟你的 職業

完成課程獲得認證

開始
廣告