本文整理汇总了C#中System.Linq.Enumerable.Max方法的典型用法代码示例。如果您正苦于以下问题:C# Enumerable.Max方法的具体用法?C# Enumerable.Max怎么用?C# Enumerable.Max使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了Enumerable.Max方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
double?[] doubles = { null, 1.5E+104, 9E+103, -2E+103 };
double? max = doubles.Max();
Console.WriteLine("The largest number is {0}.", max);
输出:
The largest number is 1.5E+104.
示例2:
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };
long max = longs.Max();
Console.WriteLine("The largest number is {0}.", max);
输出:
The largest number is 4294967296.
示例3: MaxEx4
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
public static void MaxEx4()
{
Pet[] pets = { new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };
int max = pets.Max(pet => pet.Age + pet.Name.Length);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
输出:
The maximum pet age plus name length is 14.
示例4: if
/// <summary>
/// This class implements IComparable to be able to
/// compare one Pet to another Pet.
/// </summary>
class Pet : IComparable<Pet>
{
public string Name { get; set; }
public int Age { get; set; }
/// <summary>
/// Compares this Pet to another Pet by
/// summing each Pet's age and name length.
/// </summary>
/// <param name="other">The Pet to compare this Pet to.</param>
/// <returns>-1 if this Pet is 'less' than the other Pet,
/// 0 if they are equal,
/// or 1 if this Pet is 'greater' than the other Pet.</returns>
int IComparable<Pet>.CompareTo(Pet other)
{
int sumOther = other.Age + other.Name.Length;
int sumThis = this.Age + this.Name.Length;
if (sumOther > sumThis)
return -1;
else if (sumOther == sumThis)
return 0;
else
return 1;
}
}
public static void MaxEx3()
{
Pet[] pets = { new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };
Pet max = pets.Max();
Console.WriteLine(
"The 'maximum' animal is {0}.",
max.Name);
}
输出:
The 'maximum' animal is Barley.