Dictionary<TKey,TValue>.Clear 方法


将所有键和值从 Dictionary<TKey,TValue> 中移除。Removes all keys and values from the Dictionary<TKey,TValue>.

 virtual void Clear();
public void Clear ();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Sub Clear ()



Count属性设置为0,并且还会释放对集合的元素的其他对象的引用。The Count property is set to 0, and references to other objects from elements of the collection are also released. 容量保持不变。The capacity remains unchanged.

此方法是一个 (n) 操作的 O,其中 n 是字典的容量。This method is an O(n) operation, where n is the capacity of the dictionary.

仅限 .NET Core 3.0 +:可以安全地调用此转变方法,而不会对实例上的活动枚举器无效 Dictionary<TKey,TValue>.NET Core 3.0+ only: this mutating method may be safely called without invalidating active enumerators on the Dictionary<TKey,TValue> instance. 这并不意味着线程安全。This does not imply thread safety.