Does LINQ to objects stop processing Any() when condition is true?

前端 未结 5 1119
醉话见心
醉话见心 2021-01-04 03:21

Consider the following:

bool invalidChildren = this.Children.Any(c => !c.IsValid());

This class has a collection of child objects that h

5条回答
  •  余生分开走
    2021-01-04 03:40

    Yes it will stop after it encounters the first item for which the condition matches, in your case the first item for which c.IsValid() returns false.

    From MSDN:

    The enumeration of source is stopped as soon as the result can be determined.

提交回复
热议问题