获取或设置C#中List中指定索引处的元素
要获取或设置元素在List中指定索引,代码如下-
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<String> list = new List<String>();
list.Add("100");
list.Add("200");
list.Add("300");
list.Add("400");
list.Add("500");
list.Add("600");
list.Add("700");
list.Add("800");
list.Add("900");
list.Add("1000");
Console.WriteLine("Element at index 0 = " + list[0]);
Console.WriteLine("Element at index 1 = " + list[1]);
Console.WriteLine("Element at index 2 = " + list[2]);
Console.WriteLine("Element at index 3 = " + list[3]);
Console.WriteLine("Element at index 4 = " + list[4]);
Console.WriteLine("Element at index 5 = " + list[5]);
Console.WriteLine("Element at index 6 = " + list[6]);
Console.WriteLine("Element at index 7 = " + list[7]);
}
}输出结果
这将产生以下输出-
Element at index 0 = 100 Element at index 1 = 200 Element at index 2 = 300 Element at index 3 = 400 Element at index 4 = 500 Element at index 5 = 600 Element at index 6 = 700 Element at index 7 = 800
示例
让我们看另一个例子-
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<String> list = new List<String>();
list.Add("100");
list.Add("200");
list.Add("300");
list.Add("400");
list.Add("500");
list.Add("600");
list.Add("700");
list.Add("800");
list.Add("900");
list.Add("1000");
Console.WriteLine("Element at index 0 = " + list[0]);
Console.WriteLine("Element at index 1 = " + list[1]);
Console.WriteLine("Element at index 2 = " + list[2]);
Console.WriteLine("Element at index 3 = " + list[3]);
Console.WriteLine("Element at index 4 = " + list[4]);
Console.WriteLine("Element at index 5 = " + list[5]);
Console.WriteLine("Element at index 6 = " + list[6]);
Console.WriteLine("Element at index 7 = " + list[7]);
list[3] = "450";
Console.WriteLine("Element at index 3 [UPDATED] = " + list[3]);
}
}输出结果
这将产生以下输出-
Element at index 0 = 100 Element at index 1 = 200 Element at index 2 = 300 Element at index 3 = 400 Element at index 4 = 500 Element at index 5 = 600 Element at index 6 = 700 Element at index 7 = 800 Element at index 3 [UPDATED] = 450
热门推荐
8 留学生祝福语简短
10 幽默的新娘祝福语简短
11 饭店国庆祝福语简短
12 鼠年同事祝福语简短创意
13 客户新房祝福语简短大气
14 送给表姐中秋祝福语简短
15 婚礼红包祝福语简短霸气
16 生日弟弟祝福语 简短独特
17 男生节搞笑简短祝福语
18 祝女儿结婚简短祝福语