In LINQ, the ElementAtOrDefault operator is used to get the element at a specified index position in list/collection, and it’s same as LINQ ElementAt() method, but the only difference is it will return default value in case if the specified index position element does not exist in the list.
Following is the syntax of using the LINQ ElementAtOrDefault() method to get elements at a specified index position or a default value if the specified index position element does not exist in the collection.
C# Code
VB.NET Code
If you observe the above syntax, we get elements at a specified index position.
Following is the example of the LINQ ElementAtOrDefault() method to get elements at the specified index position.
C# Code
VB.NET Code
The above example shows that we are getting different elements from the list based on specified index positions. Here we specified index position “10” which is not in the list; in this case, it will return a default value.
Following is the result of LINQ ElementAtOrDefault() method example.
This is how we can use LINQ ElementAtOrDefault() method in c#, vb.net to get element or default value from a list based on the specified index position.