Use the IndexOf() and LastIndexOf() methods to find the value 1 in intArray : Array Index « Data Structure « C# / CSharp Tutorial






using System;

class MainClass
{

  public static void Main()
  {
    int[] intArray = {1, 2, 1, 3};
    Console.WriteLine("intArray:");
    for (int i = 0; i < intArray.Length; i++)
    {
      Console.WriteLine("intArray[" + i + "] = " +
        intArray[i]);
    }

    
    int index = Array.IndexOf(intArray, 1);
    Console.WriteLine("Array.IndexOf(intArray, 1) = " + index);
    index = Array.LastIndexOf(intArray, 1);
    Console.WriteLine("Array.LastIndexOf(intArray, 1) = " + index);
  }

}
intArray:
intArray[0] = 1
intArray[1] = 2
intArray[2] = 1
intArray[3] = 3
Array.IndexOf(intArray, 1) = 0
Array.LastIndexOf(intArray, 1) = 2








11.2.Array Index
11.2.1.Set array element value by index(subscript)
11.2.2.Demonstrate an array overrun
11.2.3.Assigning array reference variables
11.2.4.Use the IndexOf() and LastIndexOf() methods to find the value 1 in intArray
11.2.5.Use the IndexOf() and LastIndexOf() methods to find the string 'Hello' in stringArray
11.2.6.Swapping Data between Positions in an Array