使用 Lambda 表示式查詢陣列中最大元素的 C# 程式


宣告陣列 −

int[] arr = { 10, 90, 20, 19, 99, 57 };

現在要獲得陣列中最大的元素,請對 Lambda 表示式使用 Max() 方法 −

arr.Max());

以下是完整的程式碼 −

示例

 即時演示

using System;
using System.Linq;
class Demo {
   static void Main() {
      int[] arr = { 10, 90, 20, 19, 99, 57 };
      Console.WriteLine(arr.Max(element => Math.Abs(element)));
   }
}

輸出

99

更新於:2020 年 6 月 22 日

507 次瀏覽

開始您的 事業

透過完成課程獲得認證

開始學習
廣告