Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

โปรแกรม C# หาองค์ประกอบมากที่สุดจากอาร์เรย์

ใช้เมธอด Max() ของ LINQ (หรือ Array.Max) หาค่าสูงสุดในอาร์เรย์

ตัวอย่างโค้ด

using System;
using System.Linq;

class Demo
{
    static void Main()
    {
        int[] arr = { 20, 50, -35, 25, 60 };
        Console.WriteLine(arr.Max()); // 60
    }
}

ผลลัพธ์

60

รองรับ: ทุกชนิดที่ Implement IComparable (int, double, string, DateTime, ฯลฯ)