C#でコレクションを反復処理する列挙子を取得します
コレクションを反復処理する列挙子を取得するには、コードは次のとおりです-
例
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
-
C#のディクショナリを反復処理する列挙子を取得します
ディクショナリを反復処理する列挙子を取得するには、コードは次のとおりです- 例 using System; using System.Collections; using System.Collections.Generic; public class Demo { public static void Main(){ Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(1
-
C#でStringCollectionを反復処理する列挙子を取得します
StringCollectionを反復処理する列挙子を取得するには、コードは次のとおりです- 例 using System; using System.Collections.Specialized; public class Demo { public static void Main(){ StringCollection stringCol = new StringCollection(); String[] arr = new String[] { "100"