What is the difference between Last() and LastOrDefault() in Linq C#?

Both Last() and LastOrDefault() are LINQ extension methods that retrieve the last element from a sequence. The key difference is how they handle empty sequences or no matching elements: Last() throws an InvalidOperationException when no element is found, while LastOrDefault() returns the default value for the type (null for reference types, 0 for integers, etc.).

Syntax

Following is the syntax for Last() method −

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

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);

Using Last() vs LastOrDefault() with Found Elements

When elements matching the criteria exist, both methods return the same result −

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

public class Student {
    public int Id { get; set; }
    public string Name { get; set; }
}

class Program {
    static void Main() {
        var studentsList = new List<Student> {
            new Student {
                Id = 1,
                Name = "John"
            },
            new Student {
                Id = 2,
                Name = "Jack"
            },
            new Student {
                Id = 1,
                Name = "Jill"
            }
        };
        
        var lastOrDefaultStudent = studentsList.LastOrDefault(student => student.Id == 1);
        var lastStudent = studentsList.Last(student => student.Id == 1);
        
        Console.WriteLine($"LastOrDefault: {lastOrDefaultStudent.Id} {lastOrDefaultStudent.Name}");
        Console.WriteLine($"Last: {lastStudent.Id} {lastStudent.Name}");
    }
}

The output of the above code is −

LastOrDefault: 1 Jill
Last: 1 Jill

Using Last() vs LastOrDefault() with No Matching Elements

When no elements match the criteria, the behavior differs significantly −

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

public class Student {
    public int Id { get; set; }
    public string Name { get; set; }
}

class Program {
    static void Main() {
        try {
            var studentsList = new List<Student> {
                new Student {
                    Id = 1,
                    Name = "John"
                },
                new Student {
                    Id = 2,
                    Name = "Jack"
                }
            };
            
            var lastOrDefaultStudent = studentsList.LastOrDefault(student => student.Id == 3);
            var value = lastOrDefaultStudent == null ? "null" : lastOrDefaultStudent.Name;
            Console.WriteLine($"LastOrDefault: {value}");
            
            var lastStudent = studentsList.Last(student => student.Id == 3);
        }
        catch (Exception ex) {
            Console.WriteLine($"Last Exception: {ex.Message}");
        }
    }
}

The output of the above code is −

LastOrDefault: null
Last Exception: Sequence contains no matching element

Working with Value Types

For value types like integers, LastOrDefault() returns the default value (0 for int, false for bool, etc.) −

using System;
using System.Linq;

class Program {
    static void Main() {
        int[] numbers = { 10, 20, 30, 40 };
        
        // Found element
        int lastEven = numbers.LastOrDefault(x => x % 2 == 0);
        Console.WriteLine($"Last even number: {lastEven}");
        
        // Not found - returns default (0)
        int lastNegative = numbers.LastOrDefault(x => x < 0);
        Console.WriteLine($"Last negative number: {lastNegative}");
        
        try {
            int lastNegativeThrows = numbers.Last(x => x < 0);
        }
        catch (InvalidOperationException ex) {
            Console.WriteLine($"Exception: {ex.Message}");
        }
    }
}

The output of the above code is −

Last even number: 40
Last negative number: 0
Exception: Sequence contains no matching element

When to Use Each Method

Method Use When Behavior on No Match
Last() You are certain the sequence contains matching elements Throws InvalidOperationException
LastOrDefault() The sequence might be empty or contain no matching elements Returns default value (null, 0, false, etc.)

Conclusion

Use Last() when you are confident the sequence contains at least one matching element and want an exception if it doesn't. Use LastOrDefault() when you need safe handling of empty sequences or no matches, as it returns the default value instead of throwing an exception.

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements