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

Onida ·
更新时间:2024-09-21
· 897 次阅读

本文实例讲述了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#程序设计有所帮助。

您可能感兴趣的文章:C#使用foreach语句遍历集合类型的方法C#使用yield关键字让自定义集合实现foreach遍历的方法C#中foreach语句使用break暂停遍历的方法C#使用foreach语句遍历队列(Queue)的方法C#中用foreach语句遍历数组及将数组作为参数的用法C#使用foreach语句遍历二维数组的方法C#使用foreach遍历哈希表(hashtable)的方法C#使用foreach循环遍历数组完整实例C#使用foreach语句遍历堆栈(Stack)的方法C#在foreach遍历删除集合中元素的实现方法总结



foreach 遍历数组 C# 方法 遍历 数组

需要 登录 后方可回复, 如果你还没有账号请 注册新账号