LINQ | Element Operator | ElementAtOrDefault
The element operators are used to return a single, or a specific element from the sequence or collection. For example, in a school when we ask, who is the principal? Then there will be only one person that will be the principal of the school. So the number of students is a collection and the principal is the only result that comes from the collection.
The LINQ Standard Query Operator supports 8 types of element operators:
The ElementAtOrDefault operator is used to return an element from the particular index from the given collection or sequence and it provides a default value if the index is out of range. It overcomes the ArgumentOutOfRangeException problem of the ElementAt operator.
- It does not support query syntax in C# and VB.Net languages.
- It support method syntax in both C# and VB.Net languages.
- It present in both the Queryable and Enumerable class.
- It does not throw an ArgumentOutOfRangeException when the index is out of range.
- The default value of the reference types and the nullable types is null.
Element at index 2 in sequence 1: Goat Element at index 4 in sequence 1: Element at index 6 in sequence 1: Element at index 2 in sequence 2: 55 Element at index 5 in sequence 2: 777 Element at index 8 in sequence 2: 0
Employee ID: 212