LINQ ElementAtOrDefault
LINQ ElementAtOrDefault operator is used to search the element present in a data source. It searches the element based on the index value provided. If the element is found at a specified index, the element is returned. Otherwise, it returns empty by default.
Syntax:
Where input_source is the data source that can be an array or List or any collection.
We will understand this by seeing the following examples.
As we know, Indexing starts with 0.
Example 1:
Here, we will create a list that has 10 integers and get the elements based on index numbers provided inside ElementAtOrDefault().
using System.Linq;
using System.Collections.Generic;
//create a class - Linuxhint
class Linuxhint
{
static public void Main(){
//create List named input_numbers
var input_numbers = new List() {100,200,300,456,12,34,56,78,54,44};
//return 6th value
Console.WriteLine("Element present at 6th position: "+input_numbers.ElementAtOrDefault (5));
//return 9th value
Console.WriteLine("Element present at 9th position: "+input_numbers.ElementAtOrDefault (8));
//return 1st value
Console.WriteLine("Element present at 1st position: "+input_numbers.ElementAtOrDefault (0));
//return 4th value
Console.WriteLine("Element present at 4th position: "+input_numbers.ElementAtOrDefault (3));
//return 10th value
Console.WriteLine("Element present at 10th position: "+input_numbers.ElementAtOrDefault (9));
}
}
Output:
Explanation:
1. So first, we created a list named input_numbers that holds 10 integer elements.
2. After that, we searched and displayed the below values using their index positions.
Example 2:
Here, we will create a list that has 3 strings and get the elements based on index numbers provided inside ElementAtOrDefault().
using System.Linq;
using System.Collections.Generic;
//create a class - Linuxhint
class Linuxhint
{
static public void Main(){
//create List named input_strings
var input_strings = new List() {"Linuxhint","c#","vignan"};
//return 1st value
Console.WriteLine("Element present at 1st position: "+input_strings.ElementAtOrDefault(0));
//return 3rd value
Console.WriteLine("Element present at 3rd position: "+input_strings.ElementAtOrDefault(2));
//return 6th value
Console.WriteLine("Element present at 6th position: "+input_strings.ElementAtOrDefault(5));
//return 9th value
Console.WriteLine("Element present at 9th position: "+input_strings.ElementAtOrDefault(8));
}
}
Output:
Explanation:
1. So first, we created a list named input_strings that holds 3 string elements.
2. After that, we searched and displayed the below strings using their index positions.
The index positions 6 and 9 are not present. Hence, empty is returned.
Conclusion
This is how to return the element based on the index number using ElementAtOrDefault operator available in C# – LINQ. If the element is found at index, the element is returned. If it is not found, it returns empty by default. We demonstrated two different examples to understand the concept better and make sure to use the modules using System, using System.Linq, using System.Collections.Generic in your code.