How to loop through all the elements of an array in C#?

CsharpProgrammingServer Side Programming

Firstly, set an array and initialize it −

int[] arr = new int[] {34, 56, 12};

To loop through all the elements of an array −

for (int i = 0; i < arr.Length; i++) {
   Console.WriteLine(arr[i]);
}

Let us see the complete code −

Example

using System;
public class Program {
   public static void Main() {
      int[] arr = new int[] {34, 56, 12};

      // Length
      Console.WriteLine("Length:" + arr.Length);

      for (int i = 0; i< arr.Length; i++) {
         Console.WriteLine(arr[i]);
      }
   }
}
raja
Published on 31-Aug-2018 12:52:53
Advertisements