Reputation: 13
Im trying to use Array.All or array.TrueforAll to see if all the values in my array are 1. I can seem to get it to work
bool allAreOne = Array.TrueForAll(globalVariables.singlePeriodClasses, value = 1);
but i get the error that "value" does not exist... not quite sure how to use this method.
Upvotes: 0
Views: 219
Reputation: 26665
bool allAreOne = Array.TrueForAll(
globalVariables.singlePeriodClasses,
value => value == 1);
The second parameter is the predicate that defines the conditions to check against the elements. Keep in mind that, A predicate is a function that returns true or false.
The Predicate is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. The elements of array are individually passed to the Predicate, and processing is stopped when the delegate returns false for any element.
Read more from MSDN.
Upvotes: 5
Reputation: 149078
The TrueForAll
method expects a delegate (in this case a Predicate<T>
). The easiest way to provide one is with a lambda expression (=>
). Also, take note of the difference between the assignment (=
) and equality (==
) operators:
bool allAreOne = Array.TrueForAll(
globalVariables.singlePeriodClasses,
value => value == 1);
Upvotes: 4