列表中的最大整数值

我有一个带有几个元素的List 。 我知道如果我用foreach迭代它,我可以获得所有值,但我只想要列表中的最大int值。

 var l = new List() { 1, 3, 2 }; 

假设.NET Framework 3.5或更高版本:

 var l = new List() { 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();