c#中向 List 列表中添加元素
要将元素添加到列表,代码如下:
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<String> list = new List<String>();
list.Add("One");
list.Add("Two");
list.Add("Three");
list.Add("Four");
list.Add("Five");
list.Add("Six");
list.Add("Seven");
list.Add("Eight");
Console.WriteLine("枚举器遍历列表元素...");
List<string>.Enumerator demoEnum = list.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
}
}输出结果
这将产生以下输出-
枚举器遍历列表元素... One Two Three Four Five Six Seven Eight
示例
让我们看另一个例子-
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(50);
list.Add(100);
list.Add(150);
list.Add(200);
list.Add(250);
list.Add(500);
list.Add(750);
list.Add(1000);
list.Add(1250);
list.Add(1500);
Console.WriteLine("枚举器遍历列表元素...");
List<int>.Enumerator demoEnum = list.GetEnumerator();
while (demoEnum.MoveNext()) {
int res = demoEnum.Current;
Console.WriteLine(res);
}
}
}输出结果
这将产生以下输出-
枚举器遍历列表元素... 50 100 150 200 250 500 750 1000 1250 1500
热门推荐
10 发文章祝福语简短英语
11 搞笑英译祝福语简短句
12 新年祝福语大全简短成语
13 高考成绩出祝福语 简短
15 给长辈冬至祝福语 简短
16 给新生的简短祝福语
17 孙子生日祝福语简短精辟
18 新年祝福语简短俏皮文案