ITPub博客

首页 > 应用开发 > IT综合 > C#使用foreach语句简单遍历数组

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

原创 IT综合 作者:wohewanglaojiji 时间:2015-04-11 08:32:40 0 删除 编辑

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

using System;
public class jb51demo {
 public static void Main() {
  int sum = 0;
  int[] nums = new int[10];
   
  // give nums some values codego.net/2/1/1/
  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);
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30007969/viewspace-1563779/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-11-04

  • 博文量
    5
  • 访问量
    6356