在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));
   }
}

输出

将生成以下输出 -

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

示例

让我们看另一个示例 -

 实时演示

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));
   }
}

输出

将生成以下输出 -

Is the array having fixed size? = True
Is the array read only? = False
Is the array synchronized? = False
Index of the 1st element = 0

更新于: 11-12-2019

177 次浏览

启动您的 事业

取得课程认证

开始
广告
© . All rights reserved.