Element Operators: ElementAt, ElementAtOrDefault

Element operators return a particular element from a sequence (collection).

The following table lists all the Element operators in LINQ.

Element Operators (Methods) Description
ElementAt Returns the element at a specified index in a collection
ElementAtOrDefault Returns the element at a specified index in a collection or a default value if the index is out of range.
First Returns the first element of a collection, or the first element that satisfies a condition.
FirstOrDefault Returns the first element of a collection, or the first element that satisfies a condition. Returns a default value if index is out of range.
Last Returns the last element of a collection, or the last element that satisfies a condition
LastOrDefault Returns the last element of a collection, or the last element that satisfies a condition. Returns a default value if no such element exists.
Single Returns the only element of a collection, or the only element that satisfies a condition.
SingleOrDefault Returns the only element of a collection, or the only element that satisfies a condition. Returns a default value if no such element exists or the collection does not contain exactly one element.

The ElementAt() method returns an element from the specified index from a given collection. If the specified index is out of the range of a collection then it will throw an Index out of range exception. Please note that index is a zero based index.

The ElementAtOrDefault() method also returns an element from the specified index from a collaction and if the specified index is out of range of a collection then it will return a default value of the data type instead of throwing an error.

The following example demonstrates ElementAt and ElementAtOrDefault method on primitive collection.

Example: LINQ ElementAt() and ElementAtOrDefault() - C#

IList<int> intList = new List<int>() { 10, 21, 30, 45, 50, 87 };
IList<string> strList = new List<string>() { "One", "Two", null, "Four", "Five" };

Console.WriteLine("1st Element in intList: {0}", intList.ElementAt(0));
Console.WriteLine("1st Element in strList: {0}", strList.ElementAt(0));
		
Console.WriteLine("2nd Element in intList: {0}", intList.ElementAt(1));
Console.WriteLine("2nd Element in strList: {0}", strList.ElementAt(1));
		
Console.WriteLine("3rd Element in intList: {0}", intList.ElementAtOrDefault(2));
Console.WriteLine("3rd Element in strList: {0}", strList.ElementAtOrDefault(2));

Console.WriteLine("10th Element in intList: {0} - default int value", 
                intList.ElementAtOrDefault(9));		
Console.WriteLine("10th Element in strList: {0} - default string value (null)",
                 strList.ElementAtOrDefault(9));		
		
		
Console.WriteLine("intList.ElementAt(9) throws an exception: Index out of range");
Console.WriteLine("-------------------------------------------------------------");
Console.WriteLine(intList.ElementAt(9));

Output:
1st Element in intList: 10
1st Element in strList: One
2nd Element in intList: 21
2nd Element in strList:
3rd Element in intList: 30
3rd Element in strList: Three
10th Element in intList: 0 - default int value
10th Element in strList: - default string value (null)
intList.ElementAt(9) throws an exception: Index out of range
-------------------------------------------------------------
Run-time exception: Index was out of range....

As you can see in the above example, intList.ElementAtOrDefault(9) returns 0 (default value of int) because intList does not include 10th element. However intList.ElementAt(9) throws "Index out of range" exception.The same way, strList.ElementAtOrDefault(9) returns null which is default value of string type. (console display empty space because it cannot display null)

Thus, it is advisable to use the ElementAtOrDefault extension method to eliminate the possibility of a runtime exception.

Learn about another element operator First and FirstOrDefault in the next section.