C# Array Exists

Description

Array Existsdetermines whether the specified array contains elements that match the conditions defined by the specified predicate.

Syntax

Array.Exists has the following syntax.


public static bool Exists<T>(
  T[] array,
  Predicate<T> match
)

Parameters

Array.Exists has the following parameters.

  • T - The type of the elements of the array.
  • array - The one-dimensional, zero-based Array to search.
  • match - The Predicate that defines the conditions of the elements to search for.

Returns

Array.Exists method returns true if array contains one or more elements that match the conditions defined by the specified predicate; otherwise, false.

Example


// ww w.  ja va 2s.  c om
using System;

public class MainClass
{
    public static void Main()
    {
        string[] myValues ={
            "1234", "19", "3456",
            "3456", "200","5678",
            "6543", "9876"
        };

        Predicate<string> myPredicate = LenCheck;
        Console.WriteLine(Array.Exists(myValues, myPredicate));


    }
    private static bool LenCheck(String s)
    {
        if (s.Length > 3)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version