The LINQ Element Methods or Operators are used to return a single element from a data source using the index of the element or using a predicate i.e. a condition. These Element Operators can be used with a single data source or on a query of multiple data sources. 

LINQ ElementAt Method: 

The LINQ ElementAt Method in C# is used to return the element at a specified index in a sequence. If the data source is empty or if the provided index value is out of range, then we will get ArgumentOutOfRangeException. If the Data Source is Null, then it will throw ArgumentNullException. If you go to the definition of the ElementAt method, then you will see the following signature.

LINQ ElementAt Method

As you can see, this method takes one parameter i.e. the index position. Then it will return the element present in that index position of the data source. There is no overloaded version available for this method.

Example to Understand LINQ ElementAt Method in C#.

Let us see an example to Understand LINQ ElementAt Method in C# with both Method and Query Syntax. Our requirement is to fetch the Element Present in Index Position 1 using ElementAt Method. For a better understanding, please have a look at the following example. Here we have created one data source which contains integer numbers. Then we fetch the element present in index position 1 by using the ElementAt method and to that method, we pass the value 1. There is no such operator called ElementAt available to write the Query Syntax, If you want then you can combine both the method syntax and query syntax to write the code which is also shown in the below example.

Run the application and it will print 2 in the output window as in index position 1 the value 2 is there.

 

What happens in the Index Value is out of the range of the collection?

If we are passing the Index value to the ElementAt method which is out of the Range of the collection, or if we pass a negative value to the ElementAt method, then the ElementAt method will throw ArgumentOutOfRangeException. That means it will throw ArgumentOutOfRangeException when the index is less than 0 or greater than or equal to the number of elements in the source. Let us understand this with an example. Please have a look at the following example. Here, the data source or collection contains 10 elements so the index is going to start from 0 to 9. Now let’s see what happens when we try to fetch the element from index position 10 or try to pass a negative index value as shown below in the below example.

Output:

What happens in the Index Value is out of the range of the collection

What happens If we call the LINQ ElementAt method on an Empty Data Source in C#?

If we call the LINQ ElementAt method on an Empty Data Source in C#, then it will throw ArgumentOutOfRangeException. Let us understand this with an example. Please have a look at the following example. Here, the data source or collection is Empty and on the Empty Data Source, we are calling the LINQ ElementAt Method.

Output:

What happens If we call the LINQ ElementAt method on an Empty Data Source in C#

What happens If we call the LINQ ElementAt method on a Data Source which is Null?

If we call the LINQ ElementAt method on a Data Source which is Null, then it will throw ArgumentNullException. Let us understand this with an example. Please have a look at the following example. Here, the data source or collection is Null and on the Null Data Source, we are calling the LINQ ElementAt Method.

Output:

What happens If we call the LINQ ElementAt method on a Data Source which is Null

While working with LINQ ElementAt Method in C#, we will get runtime ArgumentOutOfRangeException exceptions in the following scenarios.

  1. If the Data Source is empty. ArgumentOutOfRangeException
  2. If you specify a negative value for the index position. ArgumentOutOfRangeException
  3. If you specify the index position which is out of range. ArgumentOutOfRangeException

If you don’t want the Runtime ArgumentOutOfRangeException Exception, instead you want to return a default value, then you need to use the LINQ ElementAtOrDefault method.

LINQ ElementAtOrDefault Method in C#:

The LINQ ElementAtOrDefault method in C# exactly does the same thing as the LINQ ElementAt method except that this method does not throw an ArgumentOutOfRangeException exception when the data source is empty or when the supplied index value is out of range or when you specify a negative value for the index position. In such cases, it will return the default value based on the data type of the element the data source contain. If the Data Source is Null, then it will throw ArgumentNullException.

That means this method returns the default value (based on the data source data type) if the index is outside the bounds of the source sequence; otherwise, the element is at the specified position in the source sequence. If you go to the definition of the ElementAtOrDefault method, then you will see the following signature. 

LINQ ElementAtOrDefault Method in C#

Note: Like the LINQ ElementAt method, the ElementAtOrDefault method also does not have an overloaded version. Let us understand this method with Examples.

Example to Understand LINQ ElementAtOrDefault Method in C#.

Let us see an example to Understand LINQ ElementAtOrDefault Method in C# with both Method and Query Syntax. Our requirement is to fetch the Element Present in Index Position 1 using ElementAtOrDefault Method. For a better understanding, please have a look at the following example. Here we have created one data source which contains integer numbers. Then we fetch the element present in index position 1 by using the ElementAtOrDefault method and to that method, we pass the value 1. There is no such operator called ElementAtOrDefault available to write the Query Syntax, If you want then you can combine both the method syntax and query syntax to write the code which is also shown in the below example.

Output: 2

 

What happens in the Index Value is out of the Range of the Collection?

Please have a look at the following example for a better understanding. Here, the data source or collection contains 10 elements so the index is going to start from 0 to 9. Now let’s see what happens when we try to fetch the element from index position 10 or try to pass a negative index value as shown below in the below example. In this case, as we are using the ElementAtOrDefault method, it will not throw any exception, instead, it will return the default value based on the data type of the data source. Here, the collection data type is Integer and the default value of Integer is 0. So, in the below example, it will return 0.

 

LINQ ElementAt and ElementAtOrDefault Methods in C# with Complex Type:

Let us Understand LINQ ElementAt and ElementAtOrDefault Methods in C# with Complex Type. For this, we are going to use the following Student class. So, first, create a class file with the name Student.cs and then copy and paste the following code into it. It is a very simple class having 3 properties and one method which is to return a collection of students.

Next, modify the Main method of the Program class as follows. In the below example, I am showing how to use both LINQ ElementAt and ElementAtOrDefault Methods in C# with Complex Type using both Method and Query Syntax.

Output: ID: 2, Name: Priyanka, Department: HR

What is the Difference Between the LINQ ElementAt and ElementAtOrDefault Methods in C#?

Both methods are used to return an element from the specified index of a data source. But if the element is not available at the specified index position or if the data source is empty or if we specified a negative value for the Index position, then the ElementAt method will throw an ArgumentOutOfRangeException exception while the ElementAtOrDefault method will not throw an exception instead it returns a default value based on the data type of the data source elements.

In the next article, I am going to discuss the LINQ First and FirstOrDefault Methods in C# with Examples. In this article, I try to explain the LINQ ElementAt and ElementAtOrDefault Methods in C# with Examples. I hope you understood the need and use of these two ElementAt and ElementAtOrDefault Methods in C#.

Leave a Comment