Hashtable 類別

定義

代表根據索引鍵的雜湊程式碼,所整理的索引鍵/值組集合。

public ref class Hashtable : System::Collections::IDictionary
public ref class Hashtable : ICloneable, System::Collections::IDictionary, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
public class Hashtable : System.Collections.IDictionary
public class Hashtable : ICloneable, System.Collections.IDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Serializable]
public class Hashtable : ICloneable, System.Collections.IDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class Hashtable : ICloneable, System.Collections.IDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type Hashtable = class
    interface ICollection
    interface IEnumerable
    interface IDictionary
type Hashtable = class
    interface ICollection
    interface IEnumerable
    interface IDictionary
    interface ICloneable
    interface IDeserializationCallback
    interface ISerializable
type Hashtable = class
    interface ICollection
    interface IEnumerable
    interface IDictionary
    interface ISerializable
    interface IDeserializationCallback
    interface ICloneable
[<System.Serializable>]
type Hashtable = class
    interface IDictionary
    interface ICollection
    interface IEnumerable
    interface ISerializable
    interface IDeserializationCallback
    interface ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Hashtable = class
    interface IDictionary
    interface ICollection
    interface IEnumerable
    interface ISerializable
    interface IDeserializationCallback
    interface ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Hashtable = class
    interface IDictionary
    interface ISerializable
    interface IDeserializationCallback
    interface ICloneable
    interface ICollection
    interface IEnumerable
Public Class Hashtable
Implements IDictionary
Public Class Hashtable
Implements ICloneable, IDeserializationCallback, IDictionary, ISerializable
繼承
Hashtable
衍生
屬性
實作

範例

下列範例示範如何建立、初始化和執行各種函式, Hashtable 以及如何列印出其索引鍵和值。

using namespace System;
using namespace System::Collections;

public ref class Example
{
public:
    static void Main()
    {
        // Create a new hash table.
        //
        Hashtable^ openWith = gcnew Hashtable();
        
        // Add some elements to the hash table. There are no
        // duplicate keys, but some of the values are duplicates.
        openWith->Add("txt", "notepad.exe");
        openWith->Add("bmp", "paint.exe");
        openWith->Add("dib", "paint.exe");
        openWith->Add("rtf", "wordpad.exe");

        // The Add method throws an exception if the new key is 
        // already in the hash table.
        try
        {
            openWith->Add("txt", "winword.exe");
        }
        catch(...)
        {
            Console::WriteLine("An element with Key = \"txt\" already exists.");
        }

        // The Item property is the default property, so you 
        // can omit its name when accessing elements. 
        Console::WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
        
        // The default Item property can be used to change the value
        // associated with a key.
        openWith["rtf"] = "winword.exe";
        Console::WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);

        // If a key does not exist, setting the default Item property
        // for that key adds a new key/value pair.
        openWith["doc"] = "winword.exe";

        // ContainsKey can be used to test keys before inserting 
        // them.
        if (!openWith->ContainsKey("ht"))
        {
            openWith->Add("ht", "hypertrm.exe");
            Console::WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]);
        }

        // When you use foreach to enumerate hash table elements,
        // the elements are retrieved as KeyValuePair objects.
        Console::WriteLine();
        for each( DictionaryEntry de in openWith )
        {
            Console::WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
        }

        // To get the values alone, use the Values property.
        ICollection^ valueColl = openWith->Values;
        
        // The elements of the ValueCollection are strongly typed
        // with the type that was specified for hash table values.
        Console::WriteLine();
        for each( String^ s in valueColl )
        {
            Console::WriteLine("Value = {0}", s);
        }

        // To get the keys alone, use the Keys property.
        ICollection^ keyColl = openWith->Keys;
        
        // The elements of the KeyCollection are strongly typed
        // with the type that was specified for hash table keys.
        Console::WriteLine();
        for each( String^ s in keyColl )
        {
            Console::WriteLine("Key = {0}", s);
        }

        // Use the Remove method to remove a key/value pair.
        Console::WriteLine("\nRemove(\"doc\")");
        openWith->Remove("doc");

        if (!openWith->ContainsKey("doc"))
        {
            Console::WriteLine("Key \"doc\" is not found.");
        }
    }
};

int main()
{
    Example::Main();
}

/* This code example produces the following output:

An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
Value added for key = "ht": hypertrm.exe

Key = dib, Value = paint.exe
Key = txt, Value = notepad.exe
Key = ht, Value = hypertrm.exe
Key = bmp, Value = paint.exe
Key = rtf, Value = winword.exe
Key = doc, Value = winword.exe

Value = paint.exe
Value = notepad.exe
Value = hypertrm.exe
Value = paint.exe
Value = winword.exe
Value = winword.exe

Key = dib
Key = txt
Key = ht
Key = bmp
Key = rtf
Key = doc

Remove("doc")
Key "doc" is not found.
 */
using System;
using System.Collections;

class Example
{
    public static void Main()
    {
        // Create a new hash table.
        //
        Hashtable openWith = new Hashtable();

        // Add some elements to the hash table. There are no
        // duplicate keys, but some of the values are duplicates.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // The Add method throws an exception if the new key is
        // already in the hash table.
        try
        {
            openWith.Add("txt", "winword.exe");
        }
        catch
        {
            Console.WriteLine("An element with Key = \"txt\" already exists.");
        }

        // The Item property is the default property, so you
        // can omit its name when accessing elements.
        Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);

        // The default Item property can be used to change the value
        // associated with a key.
        openWith["rtf"] = "winword.exe";
        Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);

        // If a key does not exist, setting the default Item property
        // for that key adds a new key/value pair.
        openWith["doc"] = "winword.exe";

        // ContainsKey can be used to test keys before inserting
        // them.
        if (!openWith.ContainsKey("ht"))
        {
            openWith.Add("ht", "hypertrm.exe");
            Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]);
        }

        // When you use foreach to enumerate hash table elements,
        // the elements are retrieved as KeyValuePair objects.
        Console.WriteLine();
        foreach( DictionaryEntry de in openWith )
        {
            Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
        }

        // To get the values alone, use the Values property.
        ICollection valueColl = openWith.Values;

        // The elements of the ValueCollection are strongly typed
        // with the type that was specified for hash table values.
        Console.WriteLine();
        foreach( string s in valueColl )
        {
            Console.WriteLine("Value = {0}", s);
        }

        // To get the keys alone, use the Keys property.
        ICollection keyColl = openWith.Keys;

        // The elements of the KeyCollection are strongly typed
        // with the type that was specified for hash table keys.
        Console.WriteLine();
        foreach( string s in keyColl )
        {
            Console.WriteLine("Key = {0}", s);
        }

        // Use the Remove method to remove a key/value pair.
        Console.WriteLine("\nRemove(\"doc\")");
        openWith.Remove("doc");

        if (!openWith.ContainsKey("doc"))
        {
            Console.WriteLine("Key \"doc\" is not found.");
        }
    }
}

/* This code example produces the following output:

An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
Value added for key = "ht": hypertrm.exe

Key = dib, Value = paint.exe
Key = txt, Value = notepad.exe
Key = ht, Value = hypertrm.exe
Key = bmp, Value = paint.exe
Key = rtf, Value = winword.exe
Key = doc, Value = winword.exe

Value = paint.exe
Value = notepad.exe
Value = hypertrm.exe
Value = paint.exe
Value = winword.exe
Value = winword.exe

Key = dib
Key = txt
Key = ht
Key = bmp
Key = rtf
Key = doc

Remove("doc")
Key "doc" is not found.
 */
Imports System.Collections

Module Example

    Sub Main()

        ' Create a new hash table.
        '
        Dim openWith As New Hashtable()

        ' Add some elements to the hash table. There are no 
        ' duplicate keys, but some of the values are duplicates.
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")

        ' The Add method throws an exception if the new key is 
        ' already in the hash table.
        Try
            openWith.Add("txt", "winword.exe")
        Catch
            Console.WriteLine("An element with Key = ""txt"" already exists.")
        End Try

        ' The Item property is the default property, so you 
        ' can omit its name when accessing elements. 
        Console.WriteLine("For key = ""rtf"", value = {0}.", _
            openWith("rtf"))

        ' The default Item property can be used to change the value
        ' associated with a key.
        openWith("rtf") = "winword.exe"
        Console.WriteLine("For key = ""rtf"", value = {0}.", _
            openWith("rtf"))

        ' If a key does not exist, setting the default Item property
        ' for that key adds a new key/value pair.
        openWith("doc") = "winword.exe"

        ' ContainsKey can be used to test keys before inserting 
        ' them.
        If Not openWith.ContainsKey("ht") Then
            openWith.Add("ht", "hypertrm.exe")
            Console.WriteLine("Value added for key = ""ht"": {0}", _
                openWith("ht"))
        End If

        ' When you use foreach to enumerate hash table elements,
        ' the elements are retrieved as KeyValuePair objects.
        Console.WriteLine()
        For Each de As DictionaryEntry In openWith
            Console.WriteLine("Key = {0}, Value = {1}", _
                de.Key, de.Value)
        Next de

        ' To get the values alone, use the Values property.
        Dim valueColl As ICollection = openWith.Values

        ' The elements of the ValueCollection are strongly typed
        ' with the type that was specified for hash table values.
        Console.WriteLine()
        For Each s As String In valueColl
            Console.WriteLine("Value = {0}", s)
        Next s

        ' To get the keys alone, use the Keys property.
        Dim keyColl As ICollection = openWith.Keys

        ' The elements of the KeyCollection are strongly typed
        ' with the type that was specified for hash table keys.
        Console.WriteLine()
        For Each s As String In keyColl
            Console.WriteLine("Key = {0}", s)
        Next s

        ' Use the Remove method to remove a key/value pair.
        Console.WriteLine(vbLf + "Remove(""doc"")")
        openWith.Remove("doc")

        If Not openWith.ContainsKey("doc") Then
            Console.WriteLine("Key ""doc"" is not found.")
        End If

    End Sub

End Module

' This code example produces the following output:
'
'An element with Key = "txt" already exists.
'For key = "rtf", value = wordpad.exe.
'For key = "rtf", value = winword.exe.
'Value added for key = "ht": hypertrm.exe
'
'Key = dib, Value = paint.exe
'Key = txt, Value = notepad.exe
'Key = ht, Value = hypertrm.exe
'Key = bmp, Value = paint.exe
'Key = rtf, Value = winword.exe
'Key = doc, Value = winword.exe
'
'Value = paint.exe
'Value = notepad.exe
'Value = hypertrm.exe
'Value = paint.exe
'Value = winword.exe
'Value = winword.exe
'
'Key = dib
'Key = txt
'Key = ht
'Key = bmp
'Key = rtf
'Key = doc
'
'Remove("doc")
'Key "doc" is not found.
# Create new hash table using PowerShell syntax
$OpenWith = @{}

# Add one element to the hash table using the Add method
$OpenWith.Add('txt', 'notepad.exe')

# Add three eleements using PowerShell syntax three different ways
$OpenWith.dib = 'paint.exe'

$KeyBMP = 'bmp'
$OpenWith[$KeyBMP] = 'paint.exe'

$OpenWith += @{'rtf' = 'wordpad.exe'}

# Display hash table
"There are {0} in the `$OpenWith hash table as follows:" -f $OpenWith.Count
''

# Display hashtable properties
'Count of items in the hashtable  : {0}' -f $OpenWith.Count
'Is hashtable fixed size?         : {0}' -f $OpenWith.IsFixedSize
'Is hashtable read-only?          : {0}' -f $OpenWith.IsReadonly
'Is hashtabale synchronised?      : {0}' -f $OpenWith.IsSynchronized
''
'Keys in hashtable:'
$OpenWith.Keys
''
'Values in hashtable:'
$OpenWith.Values
''

<#
This script produces the following output:

There are 4 in the $OpenWith hash table as follows:

Name                           Value                                                                            
----                           -----                                                                            
txt                            notepad.exe                                                                      
dib                            paint.exe                                                                        
bmp                            paint.exe                                                                        
rtf                            wordpad.exe                                                                      

Count of items in the hashtable  : 4
Is hashtable fixed size?         : False
Is hashtable read-only?          : False
Is hashtabale synchronised?      : False

Keys in hashtable:
txt
dib
bmp
rtf

Values in hashtable:
notepad.exe
paint.exe
paint.exe
wordpad.exe
#>

備註

每個元素都是儲存在 物件中的 DictionaryEntry 索引鍵/值組。 索引鍵不能是 null ,但值可以是 。

重要

不建議您將 類別用於 Hashtable 新的開發。 相反地,建議您使用泛型 Dictionary<TKey,TValue> 類別。 如需詳細資訊,請參閱不應該在GitHub上使用非泛型集合

若要覆寫 Object.GetHashCode 方法 (或 IHashCodeProvider 介面) ,以及 Object.Equals 方法 (或 IComparer 介面) ,則需要用來做為索引鍵 Hashtable 的物件。 方法與介面的實作必須以相同方式處理區分大小寫;否則, Hashtable 的行為可能會不正確。 例如,建立 Hashtable 時,您必須使用 CaseInsensitiveHashCodeProvider 類別 (或任何不區分大小 IHashCodeProvider 寫的實作,) 類別 CaseInsensitiveComparer (或任何不區分 IComparer 大小寫的實作) 。

此外,當索引鍵存在於 Hashtable 中時,使用相同的參數呼叫時,這些方法必須產生相同的結果。 替代方法是搭配參數使用建 Hashtable 構函 IEqualityComparer 式。 如果索引鍵相等只是參考相等,則 繼承的 Object.GetHashCodeObject.Equals 實作就已足夠。

索引鍵物件必須不可變,只要它們當做 中的 Hashtable 索引鍵使用即可。

當專案加入 至 Hashtable 時,專案會根據索引鍵的雜湊碼放入貯體中。 後續的索引鍵查閱會使用索引鍵的雜湊碼,只搜尋一個特定貯體,因而大幅減少尋找元素所需的索引鍵比較數目。

Hashtable 負載因數決定專案與值區的最大比率。 較小的負載因數會以增加記憶體耗用量的成本,造成更快的平均查閱時間。 預設負載因數 1.0 通常會在速度和大小之間提供最佳平衡。 建立 時 Hashtable 也可以指定不同的負載因數。

隨著元素加入 至 Hashtable ,實際的負載因數 Hashtable 會增加。 當實際負載因數達到指定的負載因數時,中的 Hashtable 值區數目會自動增加為大於目前值區數目兩倍的最小質數 Hashtable

中的每個 Hashtable 索引鍵物件都必須提供自己的雜湊函式,其可藉由呼叫 GetHash 來存取。 不過,任何實作 IHashCodeProvider 的物件都可以傳遞至建 Hashtable 構函式,而且該雜湊函式會用於資料表中的所有物件。

Hashtable 容量是 可以保留的專案 Hashtable 數目。 隨著元素新增至 Hashtable ,容量會隨著重新配置而自動增加。

僅限.NET Framework: 對於非常大 Hashtable 的物件,您可以在執行時間環境中將組態專案的 true 屬性 <gcAllowVeryLargeObjects> 設定 enabled 為 ,將 64 位系統上的最大容量增加到 20 億個專案。

foreachVisual Basic) 中 C# 語言 (For Each 的 語句會傳回集合中專案類型的 物件。 因為 的每個 Hashtable 元素都是索引鍵/值組,所以專案類型不是索引鍵的類型或值的型別。 相反地,元素類型為 DictionaryEntry 。 例如:

for each(DictionaryEntry de in myHashtable)
{
    // ...
}
foreach(DictionaryEntry de in myHashtable)
{
    // ...
}
For Each de As DictionaryEntry In myHashtable
    ' ...
Next de

語句 foreach 是列舉值周圍的包裝函式,它只允許讀取集合,而不寫入集合。

由於 序列化和還原序列化 的 Hashtable 列舉值可能會導致元素重新排序,因此無法在不呼叫 Reset 方法的情況下繼續列舉。

注意

因為索引鍵可以繼承並變更其行為,所以使用 方法的比較 Equals 無法保證其絕對唯一性。

建構函式

Hashtable()

使用預設初始容量、載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。

Hashtable(IDictionary)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設載入因數、雜湊程式碼提供者和比較子。

Hashtable(IDictionary, IEqualityComparer)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設的載入因數以及指定的 IEqualityComparer 物件。

Hashtable(IDictionary, IHashCodeProvider, IComparer)
已過時。
已過時。

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設的載入因數,以及指定的雜湊程式碼提供者和比較子。 這個 API 已經過時。 請參閱 Hashtable(IDictionary, IEqualityComparer) 以瞭解替代步驟。

Hashtable(IDictionary, Single)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用指定的載入因數,以及預設的雜湊程式碼提供者和比較子。

Hashtable(IDictionary, Single, IEqualityComparer)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用指定的載入因數和 IEqualityComparer 物件。

Hashtable(IDictionary, Single, IHashCodeProvider, IComparer)
已過時。
已過時。

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。 新 Hashtable 物件的初始容量等於複製的項目數,並且此物件使用指定的載入因數、雜湊程式碼提供者和比較子。

Hashtable(IEqualityComparer)

使用預設的初始容量和載入因數,以及指定的 Hashtable 物件,初始化 IEqualityComparer 類別的空白新執行個體。

Hashtable(IHashCodeProvider, IComparer)
已過時。
已過時。
已過時。

使用預設的初始容量和載入因數,以及指定的雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。

Hashtable(Int32)

使用指定的初始容量以及預設的載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。

Hashtable(Int32, IEqualityComparer)

使用指定的初始容量和 Hashtable,以及預設的載入因數,初始化 IEqualityComparer 類別的空白新執行個體。

Hashtable(Int32, IHashCodeProvider, IComparer)
已過時。
已過時。

使用指定的初始容量、雜湊程式碼提供者、比較子和預設的載入因數,初始化 Hashtable 類別的空白新執行個體。

Hashtable(Int32, Single)

使用指定的初始容量和載入因數,以及預設的雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。

Hashtable(Int32, Single, IEqualityComparer)

使用指定的初始容量、載入因數和 Hashtable 物件,初始化 IEqualityComparer 類別的空白新執行個體。

Hashtable(Int32, Single, IHashCodeProvider, IComparer)
已過時。
已過時。

使用指定的初始容量、載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。

Hashtable(SerializationInfo, StreamingContext)

初始化 Hashtable 類別的空白新執行個體,這個執行個體可序列化,並使用指定的 SerializationInfoStreamingContext 物件。

屬性

comparer
已過時。
已過時。

取得或設定 IComparer 所使用的 Hashtable

Count

取得 Hashtable 中所包含的索引鍵/值組數目。

EqualityComparer

取得 IEqualityComparer 所使用的 Hashtable

hcp
已過時。
已過時。

取得或設定可以分配雜湊程式碼的物件。

IsFixedSize

取得值,指出 Hashtable 是否有固定的大小。

IsReadOnly

取得值,指出 Hashtable 是否唯讀。

IsSynchronized

取得值,這個值表示對 Hashtable 的存取是否同步 (安全執行緒)。

Item[Object]

取得或設定與指定之索引鍵相關聯的值。

Keys

取得含有 ICollection 中的索引鍵的 Hashtable

SyncRoot

取得可用以同步存取 Hashtable 的物件。

Values

取得 ICollection,包含 Hashtable 中的值。

方法

Add(Object, Object)

將有指定索引鍵和數值的項目加入 Hashtable

Clear()

移除 Hashtable 中的所有項目。

Clone()

建立 Hashtable 的淺層複本。

Contains(Object)

判斷 Hashtable 是否包含特定索引鍵。

ContainsKey(Object)

判斷 Hashtable 是否包含特定索引鍵。

ContainsValue(Object)

判斷 Hashtable 是否包含特定值。

CopyTo(Array, Int32)

Hashtable 元素複製到指定索引的一維 Array 執行個體。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回透過 IDictionaryEnumerator 重複的 Hashtable

GetHash(Object)

傳回指定索引鍵的雜湊程式碼。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

實作 ISerializable 介面,並傳回序列化 Hashtable 所需的資料。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
KeyEquals(Object, Object)

比較特定的 ObjectHashtable 中的特定索引鍵。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnDeserialization(Object)

實作 ISerializable 介面,並於還原序列化完成時引發還原序列化事件。

Remove(Object)

將有指定索引鍵的項目從 Hashtable 移除。

Synchronized(Hashtable)

傳回 Hashtable 同步處理的 (安全執行緒) 包裝函式。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

IEnumerable.GetEnumerator()

傳回逐一查看集合的列舉值。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

執行緒安全性

Hashtable 是執行緒安全,可供多個讀取器執行緒和單一寫入執行緒使用。 當只有其中一個執行緒執行寫入 (更新) 作業時,多執行緒可以使用執行緒安全,只要寫入器序列化為 Hashtable 即可允許無鎖定讀取。 若要支援多個寫入器,必須在 方法傳 Synchronized(Hashtable) 回的包裝函式中完成所有作業 Hashtable ,前提是沒有讀取 Hashtable 物件的執行緒。

透過集合列舉本質上不是安全線程程式。 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。

另請參閱