C# Enumerable First(IEnumerable, Func)
Description
Returns the first element in a sequence that satisfies a specified condition.
Syntax
public static TSource First<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate
)
Parameters
TSource
- The type of the elements of source.source
- An IEnumerableto return an element from. predicate
- A function to test each element for a condition.
Returns
Returns the first element in a sequence that satisfies a specified condition.
Example
The following code example demonstrates how to use First to return the first element of an array that satisfies a condition.
// w ww.j av a2s . co m
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
public static void Main(String[] argv){
int[] numbers = { 9, 34, 65, 67, 12, 19 };
int first = numbers.First(number => number > 80);
Console.WriteLine(first);
}
}