using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass { public static void Main() { int[] numbers = { 1, 11, 3, 19, 41, 65, 19 }; bool onlyOdd = numbers.All(n => n % 2 == 1); Console.WriteLine("The list contains only odd numbers: {0}", onlyOdd); } }
LINQ
All with condition
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; public class MainClass{ public static void Main(){ int[] numbers = { 2, 6, 1, 56, 102 }; Console.Write(numbers.All(e => e % 2 == 0) ? "Yes, they are" : "No, they aren't"); } }
Aggregate and Sum
using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass { public static void Main() { IEnumerable<int> intSequence = Enumerable.Range(1, 10); foreach (int item in intSequence) Console.WriteLine(item); int sum = intSequence.Aggregate(0, (s, i) => s + i); Console.WriteLine(sum); } }
Aggregate Prototype
using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass { public static void Main() { int N = 5; IEnumerable<int> intSequence = Enumerable.Range(1, N); foreach (int item in intSequence) Console.WriteLine(item); int agg = intSequence.Aggregate((av, e) => av * e); Console.WriteLine("{0}! = {1}", N, agg); } }
Use Aggregate on an array with tenary operator
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class MainClass{
public static void Main(){
int[] numbers = { 9, 3, 5, 4, 2, 6, 7, 1, 8 };
var query = numbers.Aggregate(5, (a,b) => ( (a < b) ? (a * b) : a));
}
}
[/csharp]
Use Aggregate on an array
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; public class MainClass { public static void Main() { int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var query = numbers.Aggregate((a, b) => a * b); } }