C#中的foreach循环
foreach循环为实现System.Collections.IEnumerable或System.Collections.Generic.IEnumerable<T>接口的类型的实例中的每个元素执行一个语句或语句块。
示例
让我们看一个foreach循环的例子-
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
LinkedList<int> linkedList = new LinkedList<int>();
linkedList.AddLast(25);
linkedList.AddLast(50);
linkedList.AddLast(100);
linkedList.AddLast(200);
linkedList.AddLast(400);
linkedList.AddLast(500);
linkedList.AddLast(550);
linkedList.AddLast(600);
linkedList.AddLast(800);
linkedList.AddLast(1200);
Console.WriteLine("Count of nodes = " + linkedList.Count);
foreach(int val in linkedList){
Console.WriteLine(val);
}
Console.WriteLine("Does the LinkedList has node 800? "+linkedList.Contains(800));
}
}输出结果
这将产生以下输出-
Count of nodes = 10 25 50 100 200 400 500 550 600 800 1200 Does the LinkedList has node 800? True
热门推荐
10 毛笔哥哥结婚祝福语简短
11 向国庆送祝福语简短
12 建队节祝福语简短
13 朋友喜得外孙简短祝福语
14 小店营业的祝福语简短
15 餐饮生日祝福语简短独特
16 鲜花英语祝福语卡片简短
17 男朋友暴富祝福语简短
18 婶婶拜年祝福语大全简短