From the course: .NET Essentials: Working with LINQ

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

The ElementAt method

The ElementAt method

- A lot of the collection types in .net provide an indexer. So if you know the index value of an item in the array, let's say, or list of T you can provide that number and you can receive a single item at that position. There are other types that may not provide that information, or may not have that functionality. So link also provides a method called elementat that lets you specify which position or the item, and it'll get that single item out of that position. It's easy to use, it's just elementat, and then the ordinal position. So here I start with zero five, 10, 15, 20, and so on. So this will return item at position one, and this one here will return item at position four, like so, that works and we have the same problem here that we did in the earlier examples. What if I specify an ordinal value of 22 here and try to run this, it's going to throw an exception argument out of range. So in that case, what I want to…

Contents