集合
所有实现了 ICollection 接口的类型都可称之为 集合。
继承 IEnumerable 接口,代表集合类型都可以枚举方式迭代。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
namespace System.Collections { public interface ICollection : IEnumerable { void CopyTo(Array array, int index);
int Count { get; } object SyncRoot { get; }
bool IsSynchronized { get; } } }
|
如何实现线程安全的?
1 2 3 4 5 6 7 8 9 10
|
public override void Add(object key, object? value) { lock (_table.SyncRoot) { _table.Add(key, value); } }
|
对应的泛型接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| namespace System.Collections.Generic { public interface ICollection<T> : IEnumerable<T> { int Count { get; }
bool IsReadOnly { get; }
void Add(T item); void Clear(); bool Contains(T item); void CopyTo(T[] array, int arrayIndex); bool Remove(T item); } }
|