The following example calculates the sum of all integers present in the collection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp17 { class Program { static void Main(string[] args) { int[] intNumbers = new int[] { 10, 30, 50, 40, 60, 20, 70, 90, 80, 100 }; //Using Method Syntax int MSTotal = intNumbers.Sum(); //Using Query Syntax int QSTotal = (from num in intNumbers select num).Sum(); Console.WriteLine("Sum = " + QSTotal); Console.ReadKey(); Console.ReadKey(); } } } |
Output: Sum : 550