C#list<T>类用于存储和获取元素。它可以有重复的元素。它位于System.Collections.Generic命名空间中。
让无涯教程看一个泛型List<T>类的示例,该类使用add()方法存储元素,并使用for-each循环迭代列表。
using System; using System.Collections.Generic; public class ListExample { public static void Main(string[] args) { // Create a list of strings var names = new List(); names.Add("Sonoo Jaiswal"); names.Add("Ankit"); names.Add("Peter"); names.Add("Irfan"); // Iterate list element using foreach loop foreach (var name in names) { Console.WriteLine(name); } } }
输出:
链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-list.html
来源:LearnFk无涯教程网
Sonoo Jaiswal Ankit Peter Irfan
using System; using System.Collections.Generic; public class ListExample { public static void Main(string[] args) { // Create a list of strings using collection initializer var names = new List() {"Sonoo", "Vimal", "Ratan", "Love" }; // Iterate through the list. foreach (var name in names) { Console.WriteLine(name); } } }
输出:
链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-list.html
来源:LearnFk无涯教程网
Sonoo Vimal Ratan Love
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)