LINQ ElementAtOrDefault() Method

In LINQ, ElementAtOrDefault() method is used to get the element at specified index position in the list/collection and it is same as LINQ ElementAt() method. The only difference between the both ElementAtOrDefault() and ElementAt() is that it will return default value only. On the other hand, if the specified index position of the element does not exist in the list, then in that case also this will return the default value.

Syntax of LINQ ElementAtOrDefault() Method

The syntax of using the LINQ ElementAtOrDefault() to get the element at specified index position.

From the above syntax, we are getting the elements at specified index position.

Example of LINQ ElementAtOrDefault() Method

Here is the example of LINQ ElementAtOrDefault() method to get the elements at specified index position.

From the above example, we are getting the different elements from the list based on the specified index. Here we specified the position of the index "10," which does not exist in the list; in this case, this will return the default value.

Output:

LINQ ElementAtOrDefault() Method




Latest Courses