Unity C#集合

集合分为两种:非泛型集合,泛型集合。

非泛型集合需要引入:System.Collections命名空间,其命名空间下的类有:

ArrayList表示大小根据需要动态增加的对象数组。

Hashtable表示根据键的哈希代码进行组织的键/值对的集合。

Queue表示对象的先进先出(FIFO)集合。

Stack表示对象的后进先出(LIFO)集合。

Stack stack=new Stack();

Stack<int> stack=new Stack<int>();

泛型集合需要引入:System.Collection.Generic命名空间,其命名空间下的类有:

Dictionary<TKey,TValue>表示根据键进行组织的键/值对的集合。

Dictionary<string,int> dic=new Dictionary<string,int>();

List<T>表示可根据索引访问对象的列表。提供用于对列表进行搜索,排序和修改的方法

Queue<T>表示对象的先进先出集合

SortedList<TKye,TValue>

表示根据键进行排序的键/值对的集合,而键基于的是相关 IComparer<T>实现

Stack<T>表示对象的后进先出集合

我要……

泛型集合选项

非泛型集合选项

线程安全或不可变集合选项

将项存储为键/值对以通过键进行快速查找

Dictionary<TKey, TValue>

Hashtable

(根据键的哈希代码组织的键/值对的集合。)

System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>

System.Collections.ObjectModel.ReadOnlyDictionary<TKey, TValue>

ImmutableDictionary(TKey, TValue) 类

按索引访问项

List<T>

System.Array

System.Collections.ArrayList

ImmutableList(T) 类

ImmutableArray 类

使用项先进先出 (FIFO)

Queue<T>

Queue

System.Collections.Concurrent.ConcurrentQueue<T>

ImmutableQueue(T) 类

使用数据后进先出 (LIFO)

Stack<T>

Stack

System.Collections.Concurrent.ConcurrentStack<T>

ImmutableStack(T) 类

按顺序访问项

LinkedList<T>

无建议

无建议

删除集合中的项或向集合添加项时接收通知。 (实现 INotifyPropertyChanged 和 System.Collections.Specialized.INotifyCollectionChanged)

System.Collections.ObjectModel.ObservableCollection<T>

无建议

无建议

已排序的集合

System.Collections.Generic.SortedList<TKey, TValue>

System.Collections.SortedList

ImmutableSortedDictionary(TKey, TValue) 类

ImmutableSortedSet(T) 类

数学函数的一个集

System.Collections.Generic.HashSet<T>

System.Collections.Generic.SortedSet<T>

无建议

ImmutableHashSet(T) 类

ImmutableSortedSet(T) 类