在C#中查找数组中最后一个元素的索引
要找到数组中最后一个元素的索引,代码如下-
示例
using System;
public class Demo {
public static void Main(){
string[] products = new string[] { "Andy", "Mark", "Gary", "Andre"};
Console.WriteLine("One or more name begin with 'A'? = {0}",
Array.Exists(products, ele => ele.StartsWith("A")));
Console.WriteLine("Is the array having fixed size? = " + products.IsFixedSize);
Console.WriteLine("Is the array read only? = " + products.IsReadOnly);
Console.WriteLine("Is the array synchronized? = " + products.IsSynchronized);
Console.WriteLine("Index of the 1st element = "+products.GetLowerBound(0));
Console.WriteLine("Index of the last element = "+products.GetUpperBound(0));
}
}输出结果
这将产生以下输出-
One or more name begin with 'A'? = True Is the array having fixed size? = True Is the array read only? = False Is the array synchronized? = False Index of the 1st element = 0 Index of the last element = 3
示例
现在让我们来看另一个示例-
using System;
public class Demo {
public static void Main(){
string[] products = new string[] { };
Console.WriteLine("Is the array having fixed size? = " + products.IsFixedSize);
Console.WriteLine("Is the array read only? = " + products.IsReadOnly);
Console.WriteLine("Is the array synchronized? = " + products.IsSynchronized);
Console.WriteLine("Index of the 1st element = "+products.GetLowerBound(0));
Console.WriteLine("Index of the last element = "+products.GetUpperBound(0));
}
}输出结果
这将产生以下输出-
Is the array having fixed size? = True Is the array read only? = False Is the array synchronized? = False Index of the 1st element = 0 Index of the last element = -1
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短