Kamil Dębowski
Kamil Dębowski

Kamil Dębowski

Differences between Single and First in C#

Kamil Dębowski's photo
Kamil Dębowski
·Jul 12, 2021·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

In this tutorial we will take a closer look at few methods used to retrieve items from collections:

  • Single()
  • SingleOrDefault()
  • First()
  • FirstOrDefault()

Firstly, we will prepare our list:

List<Student> students = new List<Student>();

students.Add(new Student() { Id = 1, Name = "John", Course = "Computer Science" });
students.Add(new Student() { Id = 2, Name = "Mark", Course = "Biology" });
students.Add(new Student() { Id = 3, Name = "Mark", Course = "Chemistry" });
students.Add(new Student() { Id = 4, Name = "David", Course = "Archeology" });

Single()

This method returns only one matching element or throws an exception.

If there is exactly one result it is returned:

var student = students.Single(s => s.Name == "John");
System.Console.WriteLine(student.Id);

// 1

If there are several matching elements it will throw exception:

var student = students.Single(s => s.Name == "Mark");

// System.InvalidOperationException: Sequence contains more than one matching element

If there is no matching element it will throw exception:

var student = students.Single(s => s.Name == "Bob");

// System.InvalidOperationException: Sequence contains no matching element

SingleOrDefault()

This function is similar to Single() but returns default value of some type if there is no matching element:

var student = students.Single(s => s.Name == "Bob");
System.Console.WriteLine(student == null);

// True
// student is null

Imagine that you have some list of integers and want to find the first number equals to some criteria (don't ask about reason to have such a list, it is for educational purposes only :) ):

List<int> numbers = new List<int> {1, 2, 3};

var number = numbers.SingleOrDefault(s => s == 4);
System.Console.WriteLine(number);

// 0

The default value for an integer is 0 (zero).

First()

This method returns the first matching element for provided criteria.

var student = students.First(s => s.Name == "Mark");
System.Console.WriteLine(student.Id);

// 2

If there is no matching record it throws an exception:

var student = students.First(s => s.Name == "Charlie");

// System.InvalidOperationException: Sequence contains no matching element

FirstOrDefault()

Similarly as in the case of Single and SingleOrDefault, FirstOrDefault returns the first matching element or default value for the used type if there is no found record.

var student = students.FirstOrDefault(s => s.Name == "Charlie");
System.Console.WriteLine(student == null);

// True

And here is example with primitive type:

List<int> numbers = new List<int> {1, 2, 3};

var number = numbers.FirstOrDefault(s => s == 4);
System.Console.WriteLine(number);

// 0

It is good to remember the difference and use specific methods depending on your needs. Pay attention to data types and their default values when using SingleOrDefault and FirstOrDefault methods.

Did you find this article valuable?

Support Kamil Dębowski by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this