generic <typename TKey, typename TValue>
public ref class Dictionary : System::Collections::Generic::ICollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IDictionary<TKey, TValue>, System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IReadOnlyCollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IReadOnlyDictionary<TKey, TValue>, System::Collections::IDictionary
generic <typename TKey, typename TValue>
public ref class Dictionary : System::Collections::Generic::ICollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IDictionary<TKey, TValue>, System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IReadOnlyCollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IReadOnlyDictionary<TKey, TValue>, System::Collections::IDictionary, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
generic <typename TKey, typename TValue>
public ref class Dictionary : System::Collections::Generic::ICollection<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::Generic::IDictionary<TKey, TValue>, System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::IDictionary, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
public class Dictionary<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>, System.Collections.IDictionary
public class Dictionary<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>, System.Collections.IDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class Dictionary<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.IDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class Dictionary<TKey,TValue> : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>, System.Collections.IDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type Dictionary<'Key, 'Value> = class
    interface ICollection<KeyValuePair<'Key, 'Value>>
    interface seq<KeyValuePair<'Key, 'Value>>
    interface IEnumerable
    interface IDictionary<'Key, 'Value>
    interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
    interface IReadOnlyDictionary<'Key, 'Value>
    interface ICollection
    interface IDictionary
type Dictionary<'Key, 'Value> = class
    interface ICollection<KeyValuePair<'Key, 'Value>>
    interface seq<KeyValuePair<'Key, 'Value>>
    interface IEnumerable
    interface IDictionary<'Key, 'Value>
    interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
    interface IReadOnlyDictionary<'Key, 'Value>
    interface ICollection
    interface IDictionary
    interface IDeserializationCallback
    interface ISerializable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type Dictionary<'Key, 'Value> = class
    interface IDictionary<'Key, 'Value>
    interface ICollection<KeyValuePair<'Key, 'Value>>
    interface seq<KeyValuePair<'Key, 'Value>>
    interface IDictionary
    interface ICollection
    interface IEnumerable
    interface ISerializable
    interface IDeserializationCallback
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type Dictionary<'Key, 'Value> = class
    interface IDictionary<'Key, 'Value>
    interface ICollection<KeyValuePair<'Key, 'Value>>
    interface IDictionary
    interface ICollection
    interface IReadOnlyDictionary<'Key, 'Value>
    interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
    interface seq<KeyValuePair<'Key, 'Value>>
    interface IEnumerable
    interface ISerializable
    interface IDeserializationCallback
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type Dictionary<'Key, 'Value> = class
    interface IDictionary<'Key, 'Value>
    interface ICollection<KeyValuePair<'Key, 'Value>>
    interface seq<KeyValuePair<'Key, 'Value>>
    interface IEnumerable
    interface IDictionary
    interface ICollection
    interface IReadOnlyDictionary<'Key, 'Value>
    interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
    interface ISerializable
    interface IDeserializationCallback
type Dictionary<'Key, 'Value> = class
    interface IDictionary<'Key, 'Value>
    interface ICollection<KeyValuePair<'Key, 'Value>>
    interface IReadOnlyDictionary<'Key, 'Value>
    interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
    interface seq<KeyValuePair<'Key, 'Value>>
    interface IDictionary
    interface ICollection
    interface IEnumerable
Public Class Dictionary(Of TKey, TValue)
Implements ICollection(Of KeyValuePair(Of TKey, TValue)), IDictionary, IDictionary(Of TKey, TValue), IEnumerable(Of KeyValuePair(Of TKey, TValue)), IReadOnlyCollection(Of KeyValuePair(Of TKey, TValue)), IReadOnlyDictionary(Of TKey, TValue)
Public Class Dictionary(Of TKey, TValue)
Implements ICollection(Of KeyValuePair(Of TKey, TValue)), IDeserializationCallback, IDictionary, IDictionary(Of TKey, TValue), IEnumerable(Of KeyValuePair(Of TKey, TValue)), IReadOnlyCollection(Of KeyValuePair(Of TKey, TValue)), IReadOnlyDictionary(Of TKey, TValue), ISerializable
Public Class Dictionary(Of TKey, TValue)
Implements ICollection(Of KeyValuePair(Of TKey, TValue)), IDeserializationCallback, IDictionary, IDictionary(Of TKey, TValue), IEnumerable(Of KeyValuePair(Of TKey, TValue)), ISerializable

下面的代码示例使用字符串键创建一个空 Dictionary<TKey,TValue> 字符串,并使用 Add 方法添加一些元素。 该示例演示方法在 Add 尝试添加重复键时引发 ArgumentException

该示例使用 Item[] C#) 中索引器 (属性来检索值,演示 KeyNotFoundException 在请求的键不存在时引发 ,并显示可以替换与键关联的值。

该示例演示如何使用 TryGetValue 方法作为更高效的方法来检索值,如果程序必须经常尝试在字典中,不并显示如何使用 ContainsKey 方法来测试某个键是否存在之前调用 Add 方法。

该示例演示如何枚举字典中的键和值,以及如何使用 Keys 属性和 属性单独枚举键和 Values 值。

最后,该示例演示 了 Remove 方法。

using namespace System; using namespace System::Collections::Generic; public ref class Example public: static void Main() // Create a new dictionary of strings, with string keys. Dictionary<String^, String^>^ openWith = gcnew Dictionary<String^, String^>(); // Add some elements to the dictionary. 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 dictionary. openWith->Add("txt", "winword.exe"); catch (ArgumentException^) Console::WriteLine("An element with Key = \"txt\" already exists."); // The Item property is another name for the indexer, so you // can omit its name when accessing elements. Console::WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]); // The indexer 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 indexer for that key // adds a new key/value pair. openWith["doc"] = "winword.exe"; // The indexer throws an exception if the requested key is // not in the dictionary. Console::WriteLine("For key = \"tif\", value = {0}.", openWith["tif"]); catch (KeyNotFoundException^) Console::WriteLine("Key = \"tif\" is not found."); // When a program often has to try keys that turn out not to // be in the dictionary, TryGetValue can be a more efficient // way to retrieve values. String^ value = ""; if (openWith->TryGetValue("tif", value)) Console::WriteLine("For key = \"tif\", value = {0}.", value); Console::WriteLine("Key = \"tif\" is not found."); // 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 dictionary elements, // the elements are retrieved as KeyValuePair objects. Console::WriteLine(); for each( KeyValuePair<String^, String^> kvp in openWith ) Console::WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); // To get the values alone, use the Values property. Dictionary<String^, String^>::ValueCollection^ valueColl = openWith->Values; // The elements of the ValueCollection are strongly typed // with the type that was specified for dictionary values. Console::WriteLine(); for each( String^ s in valueColl ) Console::WriteLine("Value = {0}", s); // To get the keys alone, use the Keys property. Dictionary<String^, String^>::KeyCollection^ keyColl = openWith->Keys; // The elements of the KeyCollection are strongly typed // with the type that was specified for dictionary 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. Key = "tif" is not found. Key = "tif" is not found. Value added for key = "ht": hypertrm.exe Key = txt, Value = notepad.exe Key = bmp, Value = paint.exe Key = dib, Value = paint.exe Key = rtf, Value = winword.exe Key = doc, Value = winword.exe Key = ht, Value = hypertrm.exe Value = notepad.exe Value = paint.exe Value = paint.exe Value = winword.exe Value = winword.exe Value = hypertrm.exe Key = txt Key = bmp Key = dib Key = rtf Key = doc Key = ht Remove("doc") Key "doc" is not found. // Create a new dictionary of strings, with string keys. Dictionary<string, string> openWith = new Dictionary<string, string>(); // Add some elements to the dictionary. 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 dictionary. openWith.Add("txt", "winword.exe"); catch (ArgumentException) Console.WriteLine("An element with Key = \"txt\" already exists."); // The Item property is another name for the indexer, so you // can omit its name when accessing elements. Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]); // The indexer 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 indexer for that key // adds a new key/value pair. openWith["doc"] = "winword.exe"; // The indexer throws an exception if the requested key is // not in the dictionary. Console.WriteLine("For key = \"tif\", value = {0}.", openWith["tif"]); catch (KeyNotFoundException) Console.WriteLine("Key = \"tif\" is not found."); // When a program often has to try keys that turn out not to // be in the dictionary, TryGetValue can be a more efficient // way to retrieve values. string value = ""; if (openWith.TryGetValue("tif", out value)) Console.WriteLine("For key = \"tif\", value = {0}.", value); Console.WriteLine("Key = \"tif\" is not found."); // 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 dictionary elements, // the elements are retrieved as KeyValuePair objects. Console.WriteLine(); foreach( KeyValuePair<string, string> kvp in openWith ) Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); // To get the values alone, use the Values property. Dictionary<string, string>.ValueCollection valueColl = openWith.Values; // The elements of the ValueCollection are strongly typed // with the type that was specified for dictionary values. Console.WriteLine(); foreach( string s in valueColl ) Console.WriteLine("Value = {0}", s); // To get the keys alone, use the Keys property. Dictionary<string, string>.KeyCollection keyColl = openWith.Keys; // The elements of the KeyCollection are strongly typed // with the type that was specified for dictionary 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. Key = "tif" is not found. Key = "tif" is not found. Value added for key = "ht": hypertrm.exe Key = txt, Value = notepad.exe Key = bmp, Value = paint.exe Key = dib, Value = paint.exe Key = rtf, Value = winword.exe Key = doc, Value = winword.exe Key = ht, Value = hypertrm.exe Value = notepad.exe Value = paint.exe Value = paint.exe Value = winword.exe Value = winword.exe Value = hypertrm.exe Key = txt Key = bmp Key = dib Key = rtf Key = doc Key = ht Remove("doc") Key "doc" is not found. Imports System.Collections.Generic Public Class Example Public Shared Sub Main() ' Create a new dictionary of strings, with string keys. Dim openWith As New Dictionary(Of String, String) ' Add some elements to the dictionary. 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 dictionary. 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" ' The default Item property throws an exception if the requested ' key is not in the dictionary. Console.WriteLine("For key = ""tif"", value = {0}.", _ openWith("tif")) Catch Console.WriteLine("Key = ""tif"" is not found.") End Try ' When a program often has to try keys that turn out not to ' be in the dictionary, TryGetValue can be a more efficient ' way to retrieve values. Dim value As String = "" If openWith.TryGetValue("tif", value) Then Console.WriteLine("For key = ""tif"", value = {0}.", value) Console.WriteLine("Key = ""tif"" is not found.") End If ' 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 dictionary elements, ' the elements are retrieved as KeyValuePair objects. Console.WriteLine() For Each kvp As KeyValuePair(Of String, String) In openWith Console.WriteLine("Key = {0}, Value = {1}", _ kvp.Key, kvp.Value) Next kvp ' To get the values alone, use the Values property. Dim valueColl As _ Dictionary(Of String, String).ValueCollection = _ openWith.Values ' The elements of the ValueCollection are strongly typed ' with the type that was specified for dictionary 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 _ Dictionary(Of String, String).KeyCollection = _ openWith.Keys ' The elements of the KeyCollection are strongly typed ' with the type that was specified for dictionary 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 Class ' 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. 'Key = "tif" is not found. 'Key = "tif" is not found. 'Value added for key = "ht": hypertrm.exe 'Key = txt, Value = notepad.exe 'Key = bmp, Value = paint.exe 'Key = dib, Value = paint.exe 'Key = rtf, Value = winword.exe 'Key = doc, Value = winword.exe 'Key = ht, Value = hypertrm.exe 'Value = notepad.exe 'Value = paint.exe 'Value = paint.exe 'Value = winword.exe 'Value = winword.exe 'Value = hypertrm.exe 'Key = txt 'Key = bmp 'Key = dib 'Key = rtf 'Key = doc 'Key = ht 'Remove("doc") 'Key "doc" is not found.

Dictionary<TKey,TValue> 型类提供从一组键到一组值的映射。 每次对字典的添加都包含一个值和与其关联的键。 使用键检索值的速度非常快,接近 O (1) ,因为 Dictionary<TKey,TValue> 类是作为哈希表实现的。

检索速度取决于为 TKey 指定的类型的哈希算法的质量。

只要对象用作 中的 Dictionary<TKey,TValue> 键,它就不得以任何影响其哈希值的方式更改。 根据字典的相等比较器,中的每个 Dictionary<TKey,TValue> 键都必须是唯一的。 如果键的类型 TValue 为引用类型,则键不能 null 为 ,但值可以是 。

Dictionary<TKey,TValue> 需要相等实现来确定键是否相等。 可以使用接受 comparer 参数的构造函数指定泛型接口的 IEqualityComparer<T> 实现;如果不指定实现,则使用默认的泛型相等比较器 EqualityComparer<T>.Default 。 如果 类型 TKey 实现 System.IEquatable<T> 泛型接口,则默认相等比较器使用该实现。

例如,可以使用 类提供的 StringComparer 不区分大小写的字符串比较器来创建具有不区分大小写的字符串键的字典。

Dictionary<TKey,TValue> 容量是 可以容纳的元素 Dictionary<TKey,TValue> 数。 将元素添加到 时 Dictionary<TKey,TValue> ,通过重新分配内部数组,容量会根据需要自动增加。

仅.NET Framework: 对于非常大 Dictionary<TKey,TValue> 的对象,可以通过在运行时环境中将配置元素的 <gcAllowVeryLargeObjects> 属性设置为 enabled ,将 64 位系统上的最大容量增加到 true 20 亿个元素。

出于枚举目的,字典中的每个项都被视为表示 KeyValuePair<TKey,TValue> 值及其键的结构。 项的返回顺序未定义。

foreach Visual Basic) C++ For Each 中 C# 语言 ( for each 语句返回集合中元素的类型的对象。 Dictionary<TKey,TValue> 由于 是键和值的集合,因此元素类型不是键的类型或值的类型。 相反,元素类型是 KeyValuePair<TKey,TValue> 键类型和值类型的 。 例如:

for each(KeyValuePair<String^, String^> kvp in myDictionary) Console::WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); foreach( KeyValuePair<string, string> kvp in myDictionary ) Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); For Each kvp As KeyValuePair(Of String, String) In myDictionary Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value) Next kvp

语句 foreach 是枚举器的包装器,它只允许从集合中读取,而不允许写入集合。

由于密钥可以继承且其行为发生更改,因此使用 Equals 方法的比较无法保证其绝对唯一性。

ToImmutableDictionary<TSource,TKey,TValue>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TValue>, IEqualityComparer<TKey>, IEqualityComparer<TValue>)

枚举并转换序列,然后使用指定的键和值比较器生成其内容的不可变字典。

AggregateBy<TSource,TKey,TAccumulate>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TKey,TAccumulate>, Func<TAccumulate,TSource,TAccumulate>, IEqualityComparer<TKey>)

表示键和值的集合。

GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>, TResult>, IEqualityComparer<TKey>)

根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值。 通过使用指定的比较器对键值进行比较,并且通过使用指定的函数对每个组的元素进行投影。

GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,IEnumerable<TInner>, TResult>)

基于键值等同性对两个序列的元素进行关联,并对结果进行分组。 使用默认的相等比较器对键进行比较。

GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,IEnumerable<TInner>, TResult>, IEqualityComparer<TKey>)

基于键值等同性对两个序列的元素进行关联,并对结果进行分组。 使用指定的 IEqualityComparer<T> 对键进行比较。

Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,TInner,TResult>, IEqualityComparer<TKey>)

基于匹配键对两个序列的元素进行关联。 使用指定的 IEqualityComparer<T> 对键进行比较。

线程安全性

Dictionary<TKey,TValue> 只要集合未修改,就可以同时支持多个读取器。 即便如此,通过集合枚举本质上也不是线程安全的过程。 在枚举与写入访问权限竞争的极少数情况下,必须在整个枚举期间锁定集合。 若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。

有关线程安全的替代,请参阅 ConcurrentDictionary<TKey,TValue> 类或 ImmutableDictionary<TKey,TValue> 类。

Visual Basic 中的公共静态 ( Shared ) 此类型的成员是线程安全的。

  • IDictionary<TKey,TValue>
  • SortedList<TKey,TValue>
  • KeyValuePair<TKey,TValue>
  • IEqualityComparer<T>
  • 即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅: https://aka.ms/ContentUserFeedback

    提交和查看相关反馈