Array - TrueForAll
Determines whether every element in the array matches the conditions defined by the specified predicate.
public static void Main() { String[] values1 = { "Y2K", "A2000", "DC2A6", "MMXIV", "0C3" }; String[] values2 = { "Y2", "A2000", "DC2A6", "MMXIV_0", "0C3" }; // C# Extension Method: Array - TrueForAll if (values1.TrueForAll(EndsWithANumber)) Console.WriteLine("All elements end with an integer."); else Console.WriteLine("Not all elements end with an integer."); // C# Extension Method: Array - TrueForAll if (values2.TrueForAll(EndsWithANumber)) Console.WriteLine("All elements end with an integer."); else Console.WriteLine("Not all elements end with an integer."); } private static bool EndsWithANumber(String value) { int s; return Int32.TryParse(value.Substring(value.Length - 1), out s); }
View Source
using System; public static partial class Extensions { /// <summary> /// A T[] extension method that true for all. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="array">The array to act on.</param> /// <param name="match">Specifies the match.</param> /// <returns>true if it succeeds, false if it fails.</returns> public static Boolean TrueForAll<T>(this T[] array, Predicate<T> match) { return Array.TrueForAll(array, match); } }