C# Array Find

Description

Array Find searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array.

Syntax

Array.Find has the following syntax.


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

Parameters

Array.Find 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 element to search for.

Returns

Array.Find method returns T

Example


//w  w w.  ja  v  a2 s. com
using System;
using System.Drawing;

public class Example
{
    public static void Main()
    {
        Point[] points = { new Point(100, 200), 
            new Point(150, 250), new Point(250, 375), 
            new Point(275, 395), new Point(295, 450) };

        Point first = Array.Find(points, ProductGT10);

    }
    private static bool ProductGT10(Point p)
    {
        if (p.X * p.Y > 100000)
        {
            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