c# 如何获取List中的元素,其索引大于int.MaxValue

c# 如何获取List中的元素,其索引大于int.MaxValue

C#的List为泛型集合,所属命名空间

System.Collections.Generic     
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

List<T>类是 ArrayList 类的泛型等效类。该类使用大小可按需动态增加的数组实现 IList<T> 泛型接口。 泛型的好处: 它为使用c#语言编写面向对象程序增加了极大的效力和灵活性。不会强行对值类型进行装箱和拆箱,或对引用类型进行向下强制类型转换,所以性能得到提高。

一般用法如下:

1、  List的基础、常用方法,声明:

List<T> mList = new List<T>();  //T为列表中元素类型,现在以string类型作为例子
List<string> mList = new List<string>(); 
List<T> testList =new List<T> (IEnumerable<T> collection);//以一个集合作为参数创建List string[] temArr = { "Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };
List<string> testList = new List<string>(temArr);

2、添加元素:

List. Add(T item);//添加一个元素
List.Add("John");
List. AddRange(IEnumerable<T> collection);//添加一组元素
string[] temArr = { "Ha","Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku",  "Locu" };
List.AddRange(temArr);
List.Insert(int index, T item);//在index位置添加一个元素
List.Insert(1, "Hei");

3、 遍历List中元素:

foreach (T element in mList)  T的类型与mList声明时一样
  {
       Console.WriteLine(element);
  }
foreach (string s in mList)
{
    Console.WriteLine(s);
}

4、删除元素:

List. Remove(T item);//删除一个值
mList.Remove("Hunter");
List. RemoveAt(int index);//删除下标为index的元素
mList.RemoveAt(0);
List. RemoveRange(int index, int count);//从下标index开始,删除count个元素
mList.RemoveRange(3, 2);

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答