获取在C#中遍历Collection的枚举数
要获得遍历Collection的枚举数,代码如下-
示例
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main(){
Collection<string> col = new Collection<string>();
col.Add("Andy");
col.Add("Kevin");
col.Add("John");
col.Add("Kevin");
col.Add("Mary");
col.Add("Katie");
col.Add("Barry");
col.Add("Nathan");
col.Add("Mark");
Console.WriteLine("Count of elements = "+ col.Count);
Console.WriteLine("Iterating through the collection...");
var enumerator = col.GetEnumerator();
while (enumerator.MoveNext()) {
Console.WriteLine(enumerator.Current);
}
}
}输出结果
这将产生以下输出-
Count of elements = 9 Iterating through the collection... Andy Kevin John Kevin Mary Katie Barry Nathan Mark
示例
让我们看另一个例子-
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main(){
Collection<int> col = new Collection<int>();
col.Add(100);
col.Add(200);
col.Add(300);
col.Add(400);
col.Add(500);
Console.WriteLine("Count of elements = "+ col.Count);
Console.WriteLine("Iterating through the collection...");
var enumerator = col.GetEnumerator();
while (enumerator.MoveNext()) {
Console.WriteLine(enumerator.Current);
}
}
}输出结果
这将产生以下输出-
Count of elements = 5 Iterating through the collection... 100 200 300 400 500
热门推荐
10 广西考试祝福语结婚简短
11 猪年祝福语简短小孩
12 元旦祝福语送长辈简短
13 恭喜二宝祝福语简短
14 祝福语暖心话简短
15 国庆中秋祝福语简短兄弟
16 朋友订婚的祝福语简短
17 送弟弟中秋祝福语简短
18 爱生日祝福语简短独特