检查数组的所有值是否为整数1

时间:2021-10-05 07:55:19

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

我试图使用Array.All或array.TrueforAll来查看我的数组中的所有值是否为1.我似乎可以让它工作

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.

但我得到“值”不存在的错误...不太确定如何使用此方法。

2 个解决方案

#1


5  

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.

第二个参数是谓词,它定义要检查元素的条件。请记住,谓词是一个返回true或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.

Predicate是一个方法的委托,如果传递给它的对象与委托中定义的条件匹配,则返回true。数组的元素分别传递给Predicate,当委托为任何元素返回false时停止处理。

Read more from MSDN.

从MSDN了解更多信息。

#2


4  

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:

TrueForAll方法需要一个委托(在本例中为Predicate )。提供一个的最简单方法是使用lambda表达式(=>)。另外,请注意赋值(=)和相等(==)运算符之间的区别:

bool allAreOne = Array.TrueForAll(
    globalVariables.singlePeriodClasses, 
    value => value == 1);

#1


5  

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.

第二个参数是谓词,它定义要检查元素的条件。请记住,谓词是一个返回true或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.

Predicate是一个方法的委托,如果传递给它的对象与委托中定义的条件匹配,则返回true。数组的元素分别传递给Predicate,当委托为任何元素返回false时停止处理。

Read more from MSDN.

从MSDN了解更多信息。

#2


4  

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:

TrueForAll方法需要一个委托(在本例中为Predicate )。提供一个的最简单方法是使用lambda表达式(=>)。另外,请注意赋值(=)和相等(==)运算符之间的区别:

bool allAreOne = Array.TrueForAll(
    globalVariables.singlePeriodClasses, 
    value => value == 1);