Finding the last index of an array

后端 未结 11 2181
不思量自难忘°
不思量自难忘° 2020-12-10 00:43

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

11条回答
  •  死守一世寂寞
    2020-12-10 01:10

    Array starts from index 0 and ends at n-1.

    static void Main(string[] args)
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int length = arr.Length - 1;   // starts from 0 to n-1
    
        Console.WriteLine(length);     // this will give the last index.
        Console.Read();
    }
    

提交回复
热议问题