C# Enumerable All
Description
Enumerable All
Determines whether
all elements of a sequence satisfy a condition.
Syntax
public static bool All<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate
)
Parameters
TSource
- The type of the elements of source.source
- An IEnumerablethat contains the elements to apply the predicate to. predicate
- A function to test each element for a condition.
Returns
returns true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.
Example
The following code example demonstrates how to use All to determine whether all the elements in a sequence satisfy a condition.
using System;// w w w.j a va2s . c o m
using System.Linq;
using System.Collections.Generic;
class Pet{
public string Name { get; set; }
public int Age { get; set; }
}
public class MainClass{
public static void Main(String[] argv){
Pet[] pets = { new Pet { Name="A", Age=10 },
new Pet { Name="B", Age=4 },
new Pet { Name="C", Age=6 } };
bool allStartWithB = pets.All(pet =>pet.Name.StartsWith("B"));
Console.WriteLine(
"{0} pet names start with 'B'.",
allStartWithB ? "All" : "Not all");
}
}
The code above generates the following result.