共用方式為


OrderedDictionary.Clear 方法

定義

移除集合中 OrderedDictionary 的所有元素。

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

實作

例外狀況

OrderedDictionary 合集為唯讀。

範例

以下程式碼範例示範了集合的 OrderedDictionary 修改。 在此範例中,使用該Clear方法清空 OrderedDictionary,然後重新填充。OrderedDictionary 此程式碼是更大程式碼範例的一部分,可於 OrderedDictionary

// Clear the OrderedDictionary and add new values
myOrderedDictionary.Clear();
myOrderedDictionary.Add("newKey1", "newValue1");
myOrderedDictionary.Add("newKey2", "newValue2");
myOrderedDictionary.Add("newKey3", "newValue3");

// Display the contents of the "new" Dictionary using an enumerator
IDictionaryEnumerator myEnumerator =
    myOrderedDictionary.GetEnumerator();

Console.WriteLine(
    "{0}Displaying the entries of a \"new\" OrderedDictionary.",
    Environment.NewLine);

DisplayEnumerator(myEnumerator);
' Clear the OrderedDictionary and add new values
myOrderedDictionary.Clear()
myOrderedDictionary.Add("newKey1", "newValue1")
myOrderedDictionary.Add("newKey2", "newValue2")
myOrderedDictionary.Add("newKey3", "newValue3")

' Display the contents of the "new" Dictionary Imports an enumerator
Dim myEnumerator As IDictionaryEnumerator = _
    myOrderedDictionary.GetEnumerator()

Console.WriteLine( _
    "{0}Displaying the entries of a 'new' OrderedDictionary.", _
    Environment.NewLine)

DisplayEnumerator(myEnumerator)

備註

呼叫 Clear 方法後,屬性 Count 會被設為零,並且從集合元素中對其他物件的引用也會被釋放。 呼叫此方法不會改變容量。

適用於