本文整理汇总了C#中System.Linq.Enumerable.Sum方法的典型用法代码示例。如果您正苦于以下问题:C# Enumerable.Sum方法的具体用法?C# Enumerable.Sum怎么用?C# Enumerable.Sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了Enumerable.Sum方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
float?[] points = { null, 0, 92.83F, null, 100.0F, 37.46F, 81.1F };
float? sum = points.Sum();
Console.WriteLine("Total points earned: {0}", sum);
输出:
Total points earned: 311.39
示例2:
List<float> numbers = new List<float> { 43.68F, 1.25F, 583.7F, 6.5F };
float sum = numbers.Sum();
Console.WriteLine("The sum of the numbers is {0}.", sum);
输出:
The sum of the numbers is 635.13.
示例3: SumEx1
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
public static void SumEx1()
{
List<Package> packages =
new List<Package>
{ new Package { Company = "Coho Vineyard", Weight = 25.2 },
new Package { Company = "Lucerne Publishing", Weight = 18.7 },
new Package { Company = "Wingtip Toys", Weight = 6.0 },
new Package { Company = "Adventure Works", Weight = 33.8 } };
double totalWeight = packages.Sum(pkg => pkg.Weight);
Console.WriteLine("The total weight of the packages is: {0}", totalWeight);
}
输出:
The total weight of the packages is: 83.7