Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- using System.Collections;
- using System.Collections.Generic;
- namespace System.Linq
- {
- public static partial class Enumerable
- {
- public static bool Any<TSource>(this IEnumerable<TSource> source)
- {
- if (source == null)
- {
- throw Error.ArgumentNull(nameof(source));
- }
- if (source is ICollection<TSource> collectionoft)
- {
- return collectionoft.Count > 0;
- }
- if (source is IIListProvider<TSource> listProv)
- {
- return listProv.GetCount(onlyIfCheap: true) > 0;
- }
- if (source is ICollection collection)
- {
- return collection.Count > 0;
- }
- using (IEnumerator<TSource> e = source.GetEnumerator())
- {
- return e.MoveNext();
- }
- }
- public static bool Any<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
- {
- if (source == null)
- {
- throw Error.ArgumentNull(nameof(source));
- }
- if (predicate == null)
- {
- throw Error.ArgumentNull(nameof(predicate));
- }
- if (source is IIListProvider<TSource> listProv && listProv.GetCount(onlyIfCheap: true) == 0)
- {
- return false;
- }
- if (source is ICollection collection && collection.Count == 0)
- {
- return false;
- }
- foreach (TSource element in source)
- {
- if (predicate(element))
- {
- return true;
- }
- }
- return false;
- }
- public static bool All<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
- {
- if (source == null)
- {
- throw Error.ArgumentNull(nameof(source));
- }
- if (predicate == null)
- {
- throw Error.ArgumentNull(nameof(predicate));
- }
- foreach (TSource element in source)
- {
- if (!predicate(element))
- {
- return false;
- }
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement