列表中的最大整数值< int>

我有几个元素的
List<int>
。我知道如果我用
foreach
迭代它,我可以获得所有值,但我只想要列表中的最大int值。
var l = new List<int>() { 1, 3, 2 };
    
已邀请:
假设.NET Framework 3.5或更高版本:
var l = new List<int>() { 1, 3, 2 };
var max = l.Max();
Console.WriteLine(max); // prints 3
在Enumerable类中有很多很酷的节省时间的东西。     
使用Enumerable.Max
int max = l.Max();
    
int max = (from l in list select l).Max().FirstOrDefault();
根据评论,这应该是
l.Max();
    
int max = listOfInts[0];
for(int i = 1; i < listOfInts.Count; i++) {
    max = Math.Max(max, listOfInts[i]);
}
    
using System.Linq;
using System.Collections.Generic;

int Max = list.Max();
    

要回复问题请先登录注册