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

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

问题描述

如何在 C# 中检索数组的最后一个元素?

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

推荐答案

数组有一个 Length 属性,它会给你数组的长度.由于数组索引是从零开始的,最后一项将位于 Length - 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天全站免登陆