Determines whether any element of an array satisfies a condition. - CSharp System

CSharp examples for System:Array Element

Description

Determines whether any element of an array satisfies a condition.

Demo Code

// Copyright (c) .NET Foundation. All rights reserved.
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections;
using System;/*from  w ww.  j  a  v  a 2 s  .c  o  m*/

public class Main{
        /// <summary>
        /// Determines whether any element of an array satisfies a condition.
        /// </summary>
        /// <param name="array">The array to test.</param>
        /// <param name="predicate">A function to test each element for a condition.</param>
        /// <returns>true if any element of the array satisfies the condition, and false otherwise.</returns>
        public static bool Any<TElement>(TElement[] array, Predicate<TElement> predicate)
        {
            foreach (TElement element in array)
            {
                if (predicate(element))
                {
                    return true;
                }
            }

            return false;
        }
}

Related Tutorials