SoFunction
Updated on 2025-03-01

C# Method to simply traverse arrays using foreach statement

This article example describes the method of C# using foreach statements to simply traverse arrays. Share it for your reference. The details are as follows:

using System;
public class jb51demo {
 public static void Main() {
  int sum = 0;
  int[] nums = new int[10];
 
  // give nums some values
  for(int i = 0; i < 10; i++) 
   nums[i] = i;
 
  // use foreach to display and sum the values
  foreach(int x in nums) {
   ("Value is: " + x);
   sum += x;
  }
  ("Summation: " + sum);
}

I hope this article will be helpful to everyone's C# programming.