find the last element in an array in csharp

To find the last element in a C# array, you can use indexing with the array's Length property. Here's an example:

main.cs
int[] numbers = { 4, 8, 15, 16, 23, 42 };
int lastNumber = numbers[numbers.Length - 1];

Console.WriteLine(lastNumber); // Output: 42
134 chars
5 lines

In this example, we create an integer array called numbers and initialize it with some values. We then use indexing to access the last element in the array by subtracting 1 from the Length property.

The output of this code will be 42, which is the last element in the numbers array.

related categories

gistlibby LogSnag