C# Enumerable Last(IEnumerable)

Description

Returns the last element of a sequence.

Syntax


public static TSource Last<TSource>(
  this IEnumerable<TSource> source
)

Parameters

  • TSource - The type of the elements of source.
  • source - An IEnumerable to return the last element of.

Returns

Returns the last element of a sequence.

Example

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


//  w  ww  .j a v  a 2 s.c o m
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  
    int[] numbers = { 9, 83, 12, 19 };
 
    int last = numbers.Last();
 
    Console.WriteLine(last);
 
  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable