C# 列表语法
List<datatype> NameOfList= new List<datatype>();
列表是可以通过索引访问的项目集合,并提供搜索、排序和操作列表项目的功能。 List <T> 类是 ArrayList 类的通用等效项。 但是 C# 中的 List<> 类型会动态调整大小,而数组不会动态调整大小。 我们需要在 List 声明中使用 < 和 >。 我们用于列表的命名空间是 System.Collection.Generic。 我们可以使用以下语法创建一个列表<>。
C# 中的列表提供了一个强类型的对象列表。 列表中的这些对象可以通过对象进行搜索。 列表中有许多方法可以提供排序、搜索和操作等功能。 在列表中,我们可以动态调整它的大小。
列表中的一些方法
- ForEach
- GetEnumerator
- GetHashCode
- GetRange
- GetType
- IndexOf(T)
- IndexOf(T, Int32)
- IndexOf(T, Int32, Int32)
- Insert
- InsertRange
C# 列表示例
以下代码将创建字符串类型列表。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace addlist { class Program { static void Main(string[] args) { List<int> list1 = new List<int>(); } } }
列表方法示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<string> colorList = new List<string>(); colorList.Add("Red"); colorList.Add("Green"); colorList.Add("Pink"); colorList.Add("Blue"); colorList.Add("Black"); foreach (string color in colorList) { Console.WriteLine(color); Console.ReadLine(); } } } }
日期:2020-04-11 23:03:43 来源:oir作者:oir