C# Linq LastorDefault Method

The LastOrDefault() method in C# LINQ returns the last element of a sequence, or a default value if the sequence is empty. This method prevents exceptions that would occur with Last() when called on empty collections.

Syntax

Following is the syntax for LastOrDefault() method −

public static T LastOrDefault<T>(this IEnumerable<T> source);
public static T LastOrDefault<T>(this IEnumerable<T> source, Func<T, bool> predicate);

Parameters

  • source − The sequence to return the last element from.

  • predicate − A function to test each element for a condition (optional).

Return Value

Returns the last element that satisfies the condition, or the default value for type T if no such element is found. For reference types, default is null. For value types like int, default is 0.

Using LastOrDefault() with Empty Collection

Example

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

class Demo {
    static void Main() {
        List<double> val = new List<double> { };
        double d = val.LastOrDefault();
        Console.WriteLine("Default Value = " + d);
        
        if (d == 0.0D) {
            d = 0.1D;
        }
        Console.WriteLine("Default Value changed = " + d);
    }
}

The output of the above code is −

Default Value = 0
Default Value changed = 0.1

Using LastOrDefault() with Non-Empty Collection

Example

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

class Program {
    static void Main() {
        List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
        
        int lastElement = numbers.LastOrDefault();
        Console.WriteLine("Last element: " + lastElement);
        
        List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
        string lastName = names.LastOrDefault();
        Console.WriteLine("Last name: " + lastName);
    }
}

The output of the above code is −

Last element: 50
Last name: Charlie

Using LastOrDefault() with Predicate

Example

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

class Program {
    static void Main() {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        
        // Find last even number
        int lastEven = numbers.LastOrDefault(x => x % 2 == 0);
        Console.WriteLine("Last even number: " + lastEven);
        
        // Find last number greater than 15 (doesn't exist)
        int lastGreaterThan15 = numbers.LastOrDefault(x => x > 15);
        Console.WriteLine("Last number > 15: " + lastGreaterThan15);
    }
}

The output of the above code is −

Last even number: 10
Last number > 15: 0

LastOrDefault() vs Last() Comparison

LastOrDefault() Last()
Returns default value if sequence is empty Throws InvalidOperationException if sequence is empty
Safe to use with potentially empty collections Should only be used when sequence is guaranteed to have elements
Returns null for reference types, 0 for numeric types Always returns an actual element from the sequence

Conclusion

The LastOrDefault() method provides a safe way to retrieve the last element from a sequence without throwing exceptions on empty collections. It returns the appropriate default value when no matching element is found, making it ideal for scenarios where the sequence might be empty.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements