Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Linq First() Method
The First() method in C# LINQ is used to return the first element from a collection such as arrays, lists, or any IEnumerable sequence. It throws an exception if the collection is empty.
Syntax
Following is the syntax for the First() method −
collection.First() collection.First(predicate)
Parameters
predicate (optional) − A function to test each element for a condition.
Return Value
Returns the first element in the collection or the first element that satisfies the condition. Throws InvalidOperationException if no element is found.
Using First() Without Predicate
The basic First() method returns the first element from the collection −
using System;
using System.Linq;
class Program {
static void Main() {
int[] arr = {20, 40, 60, 80, 100};
// Getting the first element
int result = arr.First();
Console.WriteLine("First element: " + result);
// Works with other collections too
string[] names = {"Alice", "Bob", "Charlie"};
string firstName = names.First();
Console.WriteLine("First name: " + firstName);
}
}
The output of the above code is −
First element: 20 First name: Alice
Using First() With Predicate
You can pass a condition to First() to get the first element that meets specific criteria −
using System;
using System.Linq;
class Program {
static void Main() {
int[] numbers = {10, 25, 30, 45, 60};
// First element greater than 20
int firstGreaterThan20 = numbers.First(x => x > 20);
Console.WriteLine("First number > 20: " + firstGreaterThan20);
// First even number
int firstEven = numbers.First(x => x % 2 == 0);
Console.WriteLine("First even number: " + firstEven);
string[] words = {"apple", "banana", "cherry", "date"};
// First word starting with 'c'
string firstWithC = words.First(w => w.StartsWith("c"));
Console.WriteLine("First word with 'c': " + firstWithC);
}
}
The output of the above code is −
First number > 20: 25 First even number: 10 First word with 'c': cherry
First() vs FirstOrDefault()
| First() | FirstOrDefault() |
|---|---|
| Throws exception if no element found | Returns default value if no element found |
| Use when you're sure element exists | Use when element might not exist |
| Better performance (no null checking) | Safer for uncertain collections |
Exception Handling with First()
Since First() throws exceptions, proper handling is important −
using System;
using System.Linq;
class Program {
static void Main() {
int[] emptyArray = {};
int[] numbers = {1, 3, 5, 7};
try {
// This will throw InvalidOperationException
int first = emptyArray.First();
Console.WriteLine("First element: " + first);
}
catch (InvalidOperationException ex) {
Console.WriteLine("Error: " + ex.Message);
}
try {
// This will also throw - no even numbers
int firstEven = numbers.First(x => x % 2 == 0);
Console.WriteLine("First even: " + firstEven);
}
catch (InvalidOperationException ex) {
Console.WriteLine("Error: No matching element found");
}
}
}
The output of the above code is −
Error: Sequence contains no elements Error: No matching element found
Conclusion
The LINQ First() method efficiently retrieves the first element from collections, with optional predicate support for conditional selection. Use First() when you're certain elements exist, otherwise consider FirstOrDefault() for safer operations that avoid exceptions.
