Use Generics to parse for an element in an Array

.Net 2.0 has a new generic method in the array class called "TrueForAll" which checks a whole array for the specified condition.

The TrueForAll method checks all the elements in the array and returns true only if the condition matches for all elements. This can replace the for/foreach loop that we write to iterate through the arrays.

The TrueForAll method stops and returns false at the first element for which the condition returns false.

e.g. int[] allowedNums= { 0,2,3,4,6,8}; public static void checkElements() { int[] allowedNums= { 0,2,3,4,6,8}; bool btestRes = Array.TrueForAll(allowedNums, CheckEvens)); Response.Write("Result = " + btestRes.ToString()); } private static bool CheckEvens(int iNum) { return (iNum%2 == 0? true : false); } //Output : Result = false Note: This Generic Method is available in .Net 2.0 and above Only.
By [)ia6l0 iii   Popularity  (833 Views)