欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#使用foreach语句简单遍历数组的方法

程序员文章站 2023-11-13 13:34:22
本文实例讲述了c#使用foreach语句简单遍历数组的方法。分享给大家供大家参考。具体如下: using system; public class jb51de...

本文实例讲述了c#使用foreach语句简单遍历数组的方法。分享给大家供大家参考。具体如下:

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) {
   console.writeline("value is: " + x);
   sum += x;
  }
  console.writeline("summation: " + sum);
}

希望本文所述对大家的c#程序设计有所帮助。