.NET API Documentation

Last

System.Linq.Enumerable

Summary

Returns the last element of a sequence.

Overloads

Enumerable.Last<TSource>(IEnumerable<TSource> source)

Returns the last element of a sequence.

Parameters

  • source IEnumerable<TSource>

    A to return the last element from.

Returns

The last element of the sequence.

Exceptions

  • ArgumentNullException

    source is null.

  • InvalidOperationException

    The sequence is empty.

Remarks

This method has deferred execution.

The return type of this method is , the same type as the elements of the sequence.

If the source sequence is empty, this method throws an .

Example

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

using System;
using System.Collections.Generic;
using System.Linq;

public class LinqLastExample
{
    public static void Main(string[] args)
    {
        int[] numbers = { 5, 4, 3, 2, 1 };

        try
        {
            int lastNumber = numbers.Last();
            Console.WriteLine($"The last number is: {lastNumber}"); // Output: The last number is: 1
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex.Message);
        }

        string[] words = { "apple", "banana", "cherry" };
        string lastWord = words.Last();
        Console.WriteLine($"The last word is: {lastWord}"); // Output: The last word is: cherry

        List<int> emptyList = new List<int>();
        try
        {
            int firstOfEmpty = emptyList.Last();
        }
        catch (InvalidOperationException)
        {
            Console.WriteLine("Cannot get the last element of an empty list."); // Output: Cannot get the last element of an empty list.
        }
    }
}

Enumerable.Last<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate)

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

Parameters

  • source IEnumerable<TSource>

    A to return the last element from.

  • predicate Func<TSource, bool>

    A function to test each element for a condition.

Returns

The last element of the sequence that satisfies the condition.

Exceptions

  • ArgumentNullException

    source is null.

  • ArgumentNullException

    predicate is null.

  • InvalidOperationException

    No element satisfies the condition in predicate.

Remarks

This method has deferred execution.

The return type of this method is , the same type as the elements of the sequence.

If no element in the sequence satisfies the condition specified by predicate, this method throws an .

Example

The following code example demonstrates how to use the Last method with a predicate to return the last even number in an array.

using System;
using System.Collections.Generic;
using System.Linq;

public class LinqLastPredicateExample
{
    public static void Main(string[] args)
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        try
        {
            int lastEvenNumber = numbers.Last(n => n % 2 == 0);
            Console.WriteLine($"The last even number is: {lastEvenNumber}"); // Output: The last even number is: 10
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex.Message);
        }

        string[] words = { "apple", "banana", "cherry", "date" };
        try
        {
            string lastWordStartingWithC = words.Last(w => w.StartsWith("c"));
            Console.WriteLine($"The last word starting with 'c' is: {lastWordStartingWithC}"); // Output: The last word starting with 'c' is: cherry
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex.Message);
        }

        int[] oddNumbers = { 1, 3, 5, 7, 9 };
        try
        {
            int lastEvenOdd = oddNumbers.Last(n => n % 2 == 0);
        }
        catch (InvalidOperationException)
        {
            Console.WriteLine("No even numbers found in the array."); // Output: No even numbers found in the array.
        }
    }
}