Finding the last index of an array

arrays, c#

Solution

The array has a `Length` property that will give you the length of the array. Since the array indices are zero-based, the last item will be at `Length - 1`.

string[] items = GetAllItems();
string lastItem = items[items.Length - 1];
int arrayLength = array.Length;

When declaring an array in C#, the number you give is the length of the array:

string[] items = new string[5]; // five items, index ranging from 0 to 4.

Problem

How do you retrieve the last element of an array in C#?

Original source

Related problems