获取在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", "200", "300", "400", "500" };
Console.WriteLine("Array elements...");
foreach (string res in arr){
Console.WriteLine(res);
}
stringCol.AddRange(arr);
Console.WriteLine("Does the specified string is in the StringCollection? = "+stringCol.Contains("800"));
Console.WriteLine("Total number of elements = "+stringCol.Count);
Console.WriteLine("Iterating through StringCollection = "+stringCol.Count);
StringEnumerator myenum = stringCol.GetEnumerator();
while (myenum.MoveNext())
Console.WriteLine(myenum.Current);
}
}输出结果
这将产生以下输出-
Array elements... 100 200 300 400 500 Does the specified string is in the StringCollection? = False Total number of elements = 5 Iterating through StringCollection = 5 100 200 300 400 500
示例
让我们看另一个例子-
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringCollection stringCol = new StringCollection();
stringCol.Add("10");
stringCol.Add("20");
stringCol.Add("30");
stringCol.Add("40");
stringCol.Add("50");
stringCol.Add("60");
stringCol.Add("70");
stringCol.Add("80");
stringCol.Add("90");
stringCol.Add("100");
Console.WriteLine("Elements...");
foreach (string res in stringCol){
Console.WriteLine(res);
}
Console.WriteLine("Iterating through StringCollection with Enumerator...");
StringEnumerator myenum = stringCol.GetEnumerator();
while (myenum.MoveNext())
Console.WriteLine(myenum.Current);
}
}输出结果
这将产生以下输出-
Elements... 10 20 30 40 50 60 70 80 90 100 Iterating through StringCollection with Enumerator... 10 20 30 40 50 60 70 80 90 100
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语