Home
C#
ElementAtOrDefault Use
Updated Dec 7, 2023
Dot Net Perls
ElementAtOrDefault. The C# ElementAtOrDefault method accesses an element. It handles an out-of-range access without throwing an exception.
ElementAtOrDefault returns a default value if the index is not present. ElementAt meanwhile will throw an error on nonexistent indexes.
Exception
LINQ
Example. In this program we create a 3-element array. The only valid indexes to access are 0, 1 and 2. We show how ElementAtOrDefault works on valid indexes, and also out-of-range indexes.
int Array
Tip On out-of-range accesses, ElementAtOrDefault returns the default value for the type, which for int is 0.
int
default
using System; using System.Linq; int[] array = { 4, 5, 6 }; // Call ElementAtOrDefault with various indexes. int a = array.ElementAtOrDefault(0); int b = array.ElementAtOrDefault(1); int c = array.ElementAtOrDefault(-1); int d = array.ElementAtOrDefault(1000); Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.WriteLine(d);
4 5 0 0
ElementAt. This method gets an element at an index. In many IEnumerable types, you cannot directly index a certain element. ElementAt can help with these types.
Here This program uses an array of 3 string literal elements. Arrays are IEnumerable collections.
Array
IEnumerable
And We can index this array through the ElementAt method in the System.Linq namespace.
Warning The method call with argument 3 throws a System.ArgumentOutOfRangeException.
ArgumentException
using System; using System.Linq; // Input array. string[] array = { "Dot", "Net", "Perls" }; // Test ElementAt for 0, 1, 2. string a = array.ElementAt(0); Console.WriteLine(a); string b = array.ElementAt(1); Console.WriteLine(b); string c = array.ElementAt(2); Console.WriteLine(c); // This is out of range. string d = array.ElementAt(3);
Dot Net Perls Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range.
Index from end. With the "^" syntax, we can access an index from the end of a collection. So "^2" will get the second-to-last element.
Note This syntax is the same as the end-indexing syntax available for lists and arrays in C#.
using System; using System.Linq; var values = new char[] {'a', 'b', 'c', 'd'}; // Use "^" to index from the end. var secondToLast = values.ElementAtOrDefault(^2); Console.WriteLine(secondToLast);
c
An important note. Conceptually, ElementAtOrDefault can make a collection be infinite. It can make every possible index returning a valid value—the default value.
And This could be useful if a collection is queried for invalid indexes, and actual stored values are not necessary for the algorithm.
These methods can transform how we access collections. ElementAtOrDefault eliminates the need for bounds-checking, but it can create extra complexity when handling the default value.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 7, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen