Mega Code Archive

 
Categories / C# Book / 04 LINQ
 

0419 Sum

using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Program { static void Main() { decimal[] numbers = { 3, 4, 8 }; decimal sumTotal = numbers.Sum(); // 15 Console.WriteLine(sumTotal); } } The output: 15 The following returns the total length of each of the strings in the names array: using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Program { static void Main() { string[] names = { "Java", "C#", "Javascript", "SQL", "Oracle", "Python", "C++", "C", "HTML", "CSS" }; int combinedLength = names.Sum(s => s.Length); // 19 Console.WriteLine(combinedLength); } } The output: 42