C# 中的 Lambda 表示式


C# 中的一個 Lambda 表示式描述一個模式。

Lambda 表示式在表示式上下文中使用 => 令牌。在宣告 Lambda 表示式時將其讀作“傳遞到”運算子。

此處,我們正在從列表中查詢第一個大於 50 的元素。

list.FindIndex(x => x > 50);

上面用到了 => 令牌。在下面顯示相同內容 −

例項

 線上演示

using System;
using System.Collections.Generic;

class Demo {
   static void Main() {
      List<int> list = new List<int> { 44, 6, 34, 23, 78 };

      int res = list.FindIndex(x => x > 50);
      Console.WriteLine("Index: "+res);
   }
}

輸出

Index: 4


更新於: 22-Jun-2020

519 瀏覽

開啟職業生涯 職業

完成課程並獲得認證

開始
廣告
© . All rights reserved.