---
title: "Explain First(), FirstOrDefault() function in LINQ with C#."  
description: "Explain First(), FirstOrDefault() function in LINQ with C#."  
author: "Amrith Chandran"  
published: 2025-02-12  
updated: 2025-02-12  
canonical: https://www.mindstick.com/forum/161154/explain-first-firstordefault-function-in-linq-with-c-sharp  
category: "linq"  
tags: ["linq", "linq function"]  
reading_time: 2 minutes  

---

# Explain First(), FirstOrDefault() function in LINQ with C#.

[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) the First() and [FirstOrDefault](https://www.mindstick.com/forum/159561/when-to-use-first-and-when-to-use-firstordefault-with-linq)() functions in LINQ with C#.

## Replies

### Reply by Ashutosh Patel

#### First() vs FirstOrDefault() in LINQ (C#)

\
Both **First()** and **FirstOrDefault()** are used to retrieve the first element from a collection, but they behave differently when no matching element is found.

#### First()

- Returns the first element in the collection that matches a given condition.
- Throws an exception (`InvalidOperationException`) if no element is found.
-

## Example-

find the first even number in the list,\

```cs
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
   static void Main()
   {
       List<int> numbers = new List<int> { 3, 7, 8, 10, 15 };
       int firstEven = numbers.First(n => n % 2 == 0);
       Console.WriteLine("First Even Number: " + firstEven);
       // Output: First Even Number: 8
   }
}
```

## Example (Throws Exception)

```cs
List<int> numbers = new List<int> { 1, 3, 5, 7 };
int firstEven = numbers.First(n => n % 2 == 0); // No even number exist.
// Throws: InvalidOperationException: "Sequence contains no matching element"
```

#### FirstOrDefault()

- Returns the first matching element, or the default value (`null` for reference types, `0` for numeric types) if no element is found.
- Does not throw an exception when no element matches.

## Example-

```cs
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
   static void Main()
   {
       List<int> numbers = new List<int> { 3, 7, 8, 10, 15 };
       int firstEven = numbers.FirstOrDefault(n => n % 2 == 0);
       Console.WriteLine("First Even Number: " + firstEven);
       // Output: First Even Number: 0 (if no even number exists)

       // Example with Strings
       List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
       string result = names.FirstOrDefault(n => n.StartsWith("Z"));
       Console.WriteLine(result == null ? "No match found" : result);
       // Output: No match found
   }
}
```

## When to Use What?

- When you are sure that at least one matching element exists, use **First()**.
- When no matching element exists, use **FirstOrDefault()** (to avoid exceptions).

**Also, Read:** [LINQ Aggregate functions](https://www.mindstick.com/articles/338503/explanation-of-all-linq-aggregate-functions)


---

Original Source: https://www.mindstick.com/forum/161154/explain-first-firstordefault-function-in-linq-with-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
