C ++和C#中的Foreach
C++中的Foreach
C++11引入了foreach循环以遍历每个元素。这是一个例子-
示例
#include <iostream>
using namespace std;
int main() {
int myArr[] = { 99, 15, 67 };
//foreach循环
for (int ele : myArr)
cout << ele << endl;
}输出结果
99 15 67
C#中的Foreach
让我们来看一个C#中的foreach示例。它遍历集合中的项目-
示例
using System;
namespace ArrayApplication {
class MyArray {
static void Main(string[] args) {
int [] n = new int[10]; /* n is an array of 10 integers */
for ( int i = 0; i < 10; i++ ) {
n[i] = i + 100;
}
/* output each array element's value */
foreach (int j in n ) {
int i = j-100;
Console.WriteLine("Element[{0}] = {1}", i, j);
}
Console.ReadKey();
}
}
}输出结果
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
热门推荐
2 祝福语简短七夕
10 兔年祝福语简短100字
11 给舍友送祝福语简短
12 新年恋人祝福语简短创意
13 简短的高考祝福语精选
14 别人怀孕的简短祝福语
15 哥哥祝福语简短霸气句子
16 给男生的简短祝福语
17 二胎祝福语简短霸气
18 所有的新年祝福语简短