列表
发表于:2025-10-13 | 分类: Dotnet
字数统计: 259 | 阅读时长: 1分钟 | 阅读量:

列表

IList 是对象的有序集合,顺序取决于列表的实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// \runtime\src\libraries\System.Private.CoreLib\src\System\Collections\IList.cs
namespace System.Collections
{
public interface IList : ICollection
{
// 提供读写列表元素的方法
object? this[int index] { get; set; }

// 添加元素到列表,返回插入的位置,位置取决于具体实现,ArrayList插入到最后,SortedList很可能不会
int Add(object? value);

// 是否包含指定元素
bool Contains(object? value);

// 移除所有元素
void Clear();

// 是否只读
bool IsReadOnly { get; }

// 是否固定大小
bool IsFixedSize { get; }

// 返回指定value所在index,不存在返回-1
int IndexOf(object? value);

// value插入到列表的指定index,index<=count
void Insert(int index, object? value);

// 从列表移除1个item
void Remove(object? value);

// 从列表移除指定index的item
void RemoveAt(int index);
}
}

对应的泛型接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// \runtime\src\libraries\System.Private.CoreLib\src\System\Collections\Generic\IList.cs
namespace System.Collections.Generic
{
public interface IList<T> : ICollection<T>
{
T this[int index] { get; set; }

int IndexOf(T item);

void Insert(int index, T item);

void RemoveAt(int index);
}
}

上一篇:
字典
下一篇:
枚举器