找到一个数组的最后一个索引 [英] Finding the last index of an array

查看:550
本文介绍了找到一个数组的最后一个索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何获取数组中的最后一个元素在C#?

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

推荐答案

该阵列有一个长度属性,会给你数组的长度。由于数组索引从零开始,最后一个项目将在长度 - 1

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;

在声明在C#中的数组,你给的数字是数组的长度:

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.

这篇关于找到一个数组的最后一个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆