How do you use a 'for loop' for accessing array elements in C#?

The for loop in C# executes a sequence of statements multiple times and is commonly used to iterate through arrays. It provides a clean way to access and manipulate array elements using an index variable that increments automatically.

Syntax

Following is the syntax for a for loop used with arrays −

for (int i = 0; i < arrayName.Length; i++) {
   // Access array element using arrayName[i]
}

The for loop has three parts −

  • Initialization: int i = 0 sets the starting index

  • Condition: i continues while index is valid

  • Increment: i++ moves to the next array element

For Loop Array Access Pattern arr[0] arr[1] arr[2] arr[3] arr[4] Index: 0 1 2 3 4 Loop iterates: i = 0, 1, 2, 3, 4... Each iteration accesses arr[i]

Using For Loop to Initialize and Display Array Elements

Example

using System;

class MyArray {
   static void Main(string[] args) {
      int[] n = new int[10]; /* n is an array of 10 integers */
      
      /* initialize elements of array n */
      for (int i = 0; i < 10; i++) {
         n[i] = i + 100;
      }
      
      /* output each array element's value */
      for (int j = 0; j < 10; j++) {
         Console.WriteLine("Element[{0}] = {1}", j, n[j]);
      }
      
      /* accessing specific element */
      int a = n[2];
      Console.WriteLine("Third element: " + a);
   }
}

The output of the above code is −

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
Third element: 102

Using Array.Length for Dynamic Array Access

Instead of hardcoding the array size, use the Length property for more flexible code −

Example

using System;

class DynamicArray {
   static void Main(string[] args) {
      string[] fruits = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
      
      Console.WriteLine("Array contains " + fruits.Length + " elements:");
      
      for (int i = 0; i < fruits.Length; i++) {
         Console.WriteLine("fruits[{0}] = {1}", i, fruits[i]);
      }
   }
}

The output of the above code is −

Array contains 5 elements:
fruits[0] = Apple
fruits[1] = Banana
fruits[2] = Cherry
fruits[3] = Date
fruits[4] = Elderberry

Processing Array Elements with Calculations

Example

using System;

class ArrayCalculation {
   static void Main(string[] args) {
      int[] numbers = {10, 20, 30, 40, 50};
      int sum = 0;
      
      Console.WriteLine("Array elements and their squares:");
      for (int i = 0; i < numbers.Length; i++) {
         int square = numbers[i] * numbers[i];
         sum += numbers[i];
         Console.WriteLine("numbers[{0}] = {1}, Square = {2}", i, numbers[i], square);
      }
      
      Console.WriteLine("Sum of all elements: " + sum);
   }
}

The output of the above code is −

Array elements and their squares:
numbers[0] = 10, Square = 100
numbers[1] = 20, Square = 400
numbers[2] = 30, Square = 900
numbers[3] = 40, Square = 1600
numbers[4] = 50, Square = 2500
Sum of all elements: 150

Key Points

  • Array indices start from 0, so the first element is array[0]

  • Use array.Length in the loop condition to avoid hardcoding array size

  • The loop variable i serves as the array index to access elements

  • Be careful not to exceed array bounds − index must be less than array.Length

Conclusion

For loops provide an efficient way to access array elements in C# using index-based iteration. They are ideal for scenarios where you need to process all elements sequentially or perform operations based on the element's position in the array.

Updated on: 2026-03-17T07:04:35+05:30

353 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements