C# Enumerable Last(IEnumerable, Func)

Description

Returns the last element of a sequence that satisfies a specified condition.

Syntax


public static TSource Last<TSource>(
  this IEnumerable<TSource> source,
  Func<TSource, bool> predicate
)

Parameters

  • TSource - The type of the elements of source.
  • source - An IEnumerable to return an element from.
  • predicate - A function to test each element for a condition.

Returns

Returns the last element of a sequence that satisfies a specified condition.

Example

The following code example demonstrates how to use Last to return the last element of an array that satisfies a condition.


//from ww w. ja v a  2s  . 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, 12, 19 };

      int last = numbers.Last(num => num > 80);

      Console.WriteLine(last);

  }
}
    




















Home »
  C# Tutorial »
    System.Linq »




Enumerable