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


更新於:2020 年 6 月 22 日

519 次瀏覽

開啟您的職業生涯

完成課程即可獲得認證

開始學習
廣告