Finding the index of first element in the array in C#


To find the index of first element in the array, the code is as follows −

Example

 Live Demo

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

Output

This will produce the following output −

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

Example

Let us see another example −

 Live Demo

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

Output

This will produce the following output −

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

Updated on: 11-Dec-2019

132 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements