0
0
C Sharp (C#)programming~5 mins

First, Single, and their OrDefault variants in C Sharp (C#)

Choose your learning style9 modes available
Introduction

These methods help you find one item in a list or collection easily. They make your code simple and clear when you want just one element.

When you want the first item from a list, like the first name in a list of students.
When you expect exactly one item that matches a condition, like finding a user by their unique ID.
When you want to avoid errors if no item is found, by using the OrDefault versions to get a safe default value.
When you want to check if a list has any matching items and get the first or single one without extra code.
When you want to write clean and readable code instead of looping through collections manually.
Syntax
C Sharp (C#)
collection.First();
collection.FirstOrDefault();
collection.Single();
collection.SingleOrDefault();

First() returns the first item and throws an error if none found.

Single() expects exactly one item and throws if zero or more than one found.

Examples
Gets the first name from the list. Throws error if list is empty.
C Sharp (C#)
var firstName = names.First();
Gets the first name or null (default) if list is empty.
C Sharp (C#)
var firstOrDefault = names.FirstOrDefault();
Gets the only user with Id 5. Throws if none or more than one found.
C Sharp (C#)
var singleUser = users.Single(u => u.Id == 5);
Gets the user with Id 5 or null if none found. Throws if more than one found.
C Sharp (C#)
var singleOrDefaultUser = users.SingleOrDefault(u => u.Id == 5);
Sample Program

This program shows how to use First, FirstOrDefault, Single, and SingleOrDefault on a list of numbers. It prints the results to the console.

C Sharp (C#)
using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new() { 10, 20, 30, 40 };
        
        int first = numbers.First();
        int firstOrDefault = numbers.FirstOrDefault();
        
        int single = numbers.Single(n => n == 20);
        int singleOrDefault = numbers.SingleOrDefault(n => n == 100);

        Console.WriteLine($"First: {first}");
        Console.WriteLine($"FirstOrDefault: {firstOrDefault}");
        Console.WriteLine($"Single (20): {single}");
        Console.WriteLine($"SingleOrDefault (100): {singleOrDefault}");
    }
}
OutputSuccess
Important Notes

Use First() when you want the first item and expect the list not to be empty.

Use FirstOrDefault() to avoid errors if the list might be empty; it returns default value (like 0 or null).

Single() is strict: it throws if there is not exactly one matching item.

SingleOrDefault() returns default if no match, but throws if more than one match.

Summary

First() gets the first item or throws if none.

FirstOrDefault() gets the first item or default if none.

Single() gets the only matching item or throws if zero or many.

SingleOrDefault() gets the only matching item or default if none, throws if many.