本文介绍了如何使用TrueForAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个清单,我想检查是否每个都设置为true.我可以运行一个循环并以这种方式进行检查,但是我想尝试使用列表的TrueForAll
方法来执行该循环.我需要一个谓词,但是找不到像这样的简单任务的清晰示例.
I have a list of bools and I want to check if every one is set to true. I can run a loop and check it that way but I want to try to do it with TrueForAll
method of a list. I need a predicate for that but I couldn't find a clear example for such a simple task as this.
推荐答案
使用 All
:
bool alltrue = listOfBools.All(b => b);
它将在第一个false
中返回false
一个.
It will return false
one the first false
.
但是,由于您实际上是在使用List<bool>
,因此您也可以按照类似的方式使用List.TrueForAll
:
However, since you are actually using a List<bool>
you can also use List.TrueForAll
in the similar way:
bool alltrue = listOfBools.TrueForAll(b => b);
但是由于限于列表,所以我更喜欢Enumerable.All
.
But since that is limited to a list i would prefer Enumerable.All
.
这篇关于如何使用TrueForAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!