ConcurrentDictionary<TKey,TValue> ConcurrentDictionary<TKey,TValue> ConcurrentDictionary<TKey,TValue> ConcurrentDictionary<TKey,TValue> Class

定义

表示可由多个线程同时访问的键/值对的线程安全集合。 Represents a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently.

generic <typename TKey, typename TValue>
public ref class ConcurrentDictionary : 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.InteropServices.ComVisible(false)]
[Serializable]
public class ConcurrentDictionary<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
type ConcurrentDictionary<'Key, 'Value> = class
    interface IDictionary<'Key, 'Value>
    interface IDictionary
    interface IReadOnlyDictionary<'Key, 'Value>
    interface ICollection<KeyValuePair<'Key, 'Value>>
    interface seq<KeyValuePair<'Key, 'Value>>
    interface IEnumerable
    interface ICollection
    interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>>
Public Class ConcurrentDictionary(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)
类型参数
TKey

字典中的键的类型。 The type of the keys in the dictionary.

TValue

字典中的值的类型。 The type of the values in the dictionary.

继承
ConcurrentDictionary<TKey,TValue>ConcurrentDictionary<TKey,TValue>ConcurrentDictionary<TKey,TValue>ConcurrentDictionary<TKey,TValue>
属性
ComVisibleAttribute SerializableAttribute
实现

示例

下面的示例演示如何构造ConcurrentDictionary<TKey,TValue>对象。The following example shows how to construct a ConcurrentDictionary<TKey,TValue> object.


class CD_Ctor
{
        // Demonstrates:
        //      ConcurrentDictionary<TKey, TValue> ctor(concurrencyLevel, initialCapacity)
        //      ConcurrentDictionary<TKey, TValue>[TKey]
        static void Main()
        {
            // We know how many items we want to insert into the ConcurrentDictionary.
            // So set the initial capacity to some prime number above that, to ensure that
            // the ConcurrentDictionary does not need to be resized while initializing it.
            int NUMITEMS = 64;
            int initialCapacity = 101;

            // The higher the concurrencyLevel, the higher the theoretical number of operations
            // that could be performed concurrently on the ConcurrentDictionary.  However, global
            // operations like resizing the dictionary take longer as the concurrencyLevel rises. 
            // For the purposes of this example, we'll compromise at numCores * 2.
            int numProcs = Environment.ProcessorCount;
            int concurrencyLevel = numProcs * 2;

            // Construct the dictionary with the desired concurrencyLevel and initialCapacity
            ConcurrentDictionary<int, int> cd = new ConcurrentDictionary<int, int>(concurrencyLevel, initialCapacity);

            // Initialize the dictionary
            for (int i = 0; i < NUMITEMS; i++) cd[i] = i * i;

            Console.WriteLine("The square of 23 is {0} (should be {1})", cd[23], 23 * 23);
        }
}
Imports System.Collections.Concurrent
Imports System.Threading.Tasks
Class CD_Ctor
    ' Demonstrates:
    ' ConcurrentDictionary<TKey, TValue> ctor(concurrencyLevel, initialCapacity)
    ' ConcurrentDictionary<TKey, TValue>[TKey]
    Shared Sub Main()
        ' We know how many items we want to insert into the ConcurrentDictionary.
        ' So set the initial capacity to some prime number above that, to ensure that
        ' the ConcurrentDictionary does not need to be resized while initializing it.
        Dim NUMITEMS As Integer = 64
        Dim initialCapacity As Integer = 101

        ' The higher the concurrencyLevel, the higher the theoretical number of operations
        ' that could be performed concurrently on the ConcurrentDictionary. However, global
        ' operations like resizing the dictionary take longer as the concurrencyLevel rises. 
        ' For the purposes of this example, we'll compromise at numCores * 2.
        Dim numProcs As Integer = Environment.ProcessorCount
        Dim concurrencyLevel As Integer = numProcs * 2

        ' Construct the dictionary with the desired concurrencyLevel and initialCapacity
        Dim cd As New ConcurrentDictionary(Of Integer, Integer)(concurrencyLevel, initialCapacity)

        ' Initialize the dictionary
        For i As Integer = 0 To NUMITEMS - 1
            cd(i) = i * i
        Next

        Console.WriteLine("The square of 23 is {0} (should be {1})", cd(23), 23 * 23)
    End Sub
End Class

注解

对于非常大ConcurrentDictionary<TKey,TValue>对象,您可以将最大数组大小增加到 2 千兆字节 (GB) 的 64 位系统上通过将配置元素设置为true在运行时环境中。For very large ConcurrentDictionary<TKey,TValue> objects, you can increase the maximum array size to 2 gigabytes (GB) on a 64-bit system by setting the configuration element to true in the run-time environment.

备注

ConcurrentDictionary<TKey,TValue> 实现IReadOnlyCollection<T>IReadOnlyDictionary<TKey,TValue>开头的接口.NET Framework 4.6.NET Framework 4.6; 在以前版本的.NET Framework 中,ConcurrentDictionary<TKey,TValue>类未实现这些接口。ConcurrentDictionary<TKey,TValue> implements the IReadOnlyCollection<T> and IReadOnlyDictionary<TKey,TValue> interfaces starting with the .NET Framework 4.6.NET Framework 4.6; in previous versions of the .NET Framework, the ConcurrentDictionary<TKey,TValue> class did not implement these interfaces.

System.Collections.Generic.Dictionary<TKey,TValue>类,ConcurrentDictionary<TKey,TValue>实现IDictionary<TKey,TValue>接口。Like the System.Collections.Generic.Dictionary<TKey,TValue> class, ConcurrentDictionary<TKey,TValue> implements the IDictionary<TKey,TValue> interface. 此外,ConcurrentDictionary<TKey,TValue>用于添加或更新在字典中,键/值对提供几种方法下, 表中所述。In addition, ConcurrentDictionary<TKey,TValue> provides several methods for adding or updating key/value pairs in the dictionary, as described in the following table.

任务To do this 使用此方法Use this method 用法说明Usage notes
如果它尚不存在字典中向字典中,添加新的密钥Add a new key to the dictionary, if it doesn’t already exist in the dictionary TryAdd 如果字典中当前不存在该键,则此方法将添加指定的键/值对。This method adds the specified key/value pair, if the key doesn’t currently exist in the dictionary. 该方法返回truefalse具体取决于是否已添加新对。The method returns true or false depending on whether the new pair was added.
如果该注册表项具有特定值,更新为现有键在字典中值Update the value for an existing key in the dictionary, if that key has a specific value TryUpdate 此方法检查是否该密钥具有指定的值,如果它,则用新值更新该键。This method checks whether the key has a specified value, and if it does, updates the key with a new value. 它相当于CompareExchange方法,它的用于字典元素不同。It's similar to the CompareExchange method, except that it's used for dictionary elements.
无条件地,在字典中存储键/值对,并覆盖已存在的键的值Store a key/value pair in the dictionary unconditionally, and overwrite the value of a key that already exists 索引器的资源库: dictionary[key] = newValueThe indexer’s setter: dictionary[key] = newValue
将键/值对添加到字典中,或如果密钥已存在,更新基于键的现有值的键的值Add a key/value pair to the dictionary, or if the key already exists, update the value for the key based on the key’s existing value AddOrUpdate(TKey, Func<TKey,TValue>, Func<TKey,TValue,TValue>)

- 或 -—or—

AddOrUpdate(TKey, TValue, Func<TKey,TValue,TValue>)
AddOrUpdate(TKey, Func<TKey,TValue>, Func<TKey,TValue,TValue>) 接受的密钥和两个委托。AddOrUpdate(TKey, Func<TKey,TValue>, Func<TKey,TValue,TValue>) accepts the key and two delegates. 它使用的第一个委托,如果键不存在字典; 中它接受键,并返回应添加的密钥的值。It uses the first delegate if the key doesn’t exist in the dictionary; it accepts the key and returns the value that should be added for the key. 如果在项存在; 它使用第二个委托它接受的键和其当前值,并返回新值应为键设置。It uses the second delegate if the key does exist; it accepts the key and its current value, and it returns the new value that should be set for the key.

AddOrUpdate(TKey, TValue, Func<TKey,TValue,TValue>) 接受键、 值添加,以及更新委托。AddOrUpdate(TKey, TValue, Func<TKey,TValue,TValue>) accepts the key, a value to add, and the update delegate. 这是上一重载相同,只不过它不使用委托来添加密钥。This is the same as the previous overload, except that it doesn't use a delegate to add a key.
获取此键在字典中,将值添加到字典并将其返回如果该键不存在的值Get the value for a key in the dictionary, adding the value to the dictionary and returning it if the key doesn’t exist GetOrAdd(TKey, TValue)

- 或 -—or—

GetOrAdd(TKey, Func<TKey,TValue>)
这些重载提供迟缓初始化为键/值对在字典中,仅当找不到添加的值。These overloads provide lazy initialization for a key/value pair in the dictionary, adding the value only if it’s not there.

GetOrAdd(TKey, TValue) 采用要添加密钥不存在的值。GetOrAdd(TKey, TValue) takes the value to be added if the key doesn’t exist.

GetOrAdd(TKey, Func<TKey,TValue>) 将一个委托,它将生成值,如果不存在。GetOrAdd(TKey, Func<TKey,TValue>) takes a delegate that will generate the value if the key doesn’t exist.

所有这些操作是原子操作,并且是线程安全的所有其他操作方面ConcurrentDictionary<TKey,TValue>类。All these operations are atomic and are thread-safe with regards to all other operations on the ConcurrentDictionary<TKey,TValue> class. 唯一的例外是,它是接受一个委托,委托的方法AddOrUpdateGetOrAddThe only exceptions are the methods that accept a delegate, that is, AddOrUpdate and GetOrAdd. 修改和向字典中,写入操作ConcurrentDictionary<TKey,TValue>使用细粒度锁定,以确保线程安全。For modifications and write operations to the dictionary, ConcurrentDictionary<TKey,TValue> uses fine-grained locking to ensure thread safety. (在字典上的读取操作执行无锁方式。)但是,外部锁以避免出现问题造成的执行锁下的未知的代码调用这些方法的委托。(Read operations on the dictionary are performed in a lock-free manner.) However, delegates for these methods are called outside the locks to avoid the problems that can arise from executing unknown code under a lock. 因此,执行这些委托的代码不是受该操作的原子性。Therefore, the code executed by these delegates is not subject to the atomicity of the operation.

构造函数

ConcurrentDictionary<TKey,TValue>() ConcurrentDictionary<TKey,TValue>() ConcurrentDictionary<TKey,TValue>() ConcurrentDictionary<TKey,TValue>()

初始化 ConcurrentDictionary<TKey,TValue> 类的新实例,该实例为空,具有默认的并发级别和默认的初始容量,并为键类型使用默认比较器。 Initializes a new instance of the ConcurrentDictionary<TKey,TValue> class that is empty, has the default concurrency level, has the default initial capacity, and uses the default comparer for the key type.

ConcurrentDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>) ConcurrentDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>) ConcurrentDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>) ConcurrentDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>)

初始化 ConcurrentDictionary<TKey,TValue> 类的新实例,该实例包含从指定的 IEnumerable<T> 中复制的元素,具有默认的并发级别和默认的初始容量,并为键类型使用默认比较器。 Initializes a new instance of the ConcurrentDictionary<TKey,TValue> class that contains elements copied from the specified IEnumerable<T>, has the default concurrency level, has the default initial capacity, and uses the default comparer for the key type.

ConcurrentDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>) ConcurrentDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>) ConcurrentDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>) ConcurrentDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>)

初始化 ConcurrentDictionary<TKey,TValue> 类的新实例,该实例包含从指定的 IEnumerable 中复制的元素,具有默认的并发级别和默认的初始容量,并使用指定的 IEqualityComparer<T> Initializes a new instance of the ConcurrentDictionary<TKey,TValue> class that contains elements copied from the specified IEnumerable has the default concurrency level, has the default initial capacity, and uses the specified IEqualityComparer<T>.

ConcurrentDictionary<TKey,TValue>(IEqualityComparer<TKey>) ConcurrentDictionary<TKey,TValue>(IEqualityComparer<TKey>) ConcurrentDictionary<TKey,TValue>(IEqualityComparer<TKey>) ConcurrentDictionary<TKey,TValue>(IEqualityComparer<TKey>)

初始化 ConcurrentDictionary<TKey,TValue> 类的新实例,该实例为空,具有默认的并发级别和容量,并使用指定的 IEqualityComparer<T> Initializes a new instance of the ConcurrentDictionary<TKey,TValue> class that is empty, has the default concurrency level and capacity, and uses the specified IEqualityComparer<T>.

ConcurrentDictionary<TKey,TValue>(Int32, IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>) ConcurrentDictionary<TKey,TValue>(Int32, IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>) ConcurrentDictionary<TKey,TValue>(Int32, IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>) ConcurrentDictionary<TKey,TValue>(Int32, IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>)

初始化 ConcurrentDictionary<TKey,TValue> 类的新实例,该实例包含从指定的 IEnumerable 中复制的元素并使用指定的 IEqualityComparer<T> Initializes a new instance of the ConcurrentDictionary<TKey,TValue> class that contains elements copied from the specified IEnumerable, and uses the specified IEqualityComparer<T>.

ConcurrentDictionary<TKey,TValue>(Int32, Int32) ConcurrentDictionary<TKey,TValue>(Int32, Int32) ConcurrentDictionary<TKey,TValue>(Int32, Int32) ConcurrentDictionary<TKey,TValue>(Int32, Int32)

初始化 ConcurrentDictionary<TKey,TValue> 类的新实例,该实例为空,具有指定的并发级别和容量,并为键类型使用默认比较器。 Initializes a new instance of the ConcurrentDictionary<TKey,TValue> class that is empty, has the specified concurrency level and capacity, and uses the default comparer for the key type.

ConcurrentDictionary<TKey,TValue>(Int32, Int32, IEqualityComparer<TKey>) ConcurrentDictionary<TKey,TValue>(Int32, Int32, IEqualityComparer<TKey>) ConcurrentDictionary<TKey,TValue>(Int32, Int32, IEqualityComparer<TKey>) ConcurrentDictionary<TKey,TValue>(Int32, Int32, IEqualityComparer<TKey>)

初始化 ConcurrentDictionary<TKey,TValue> 类的新实例,该实例为空,具有指定的并发级别和指定的初始容量,并使用指定的 IEqualityComparer<T> Initializes a new instance of the ConcurrentDictionary<TKey,TValue> class that is empty, has the specified concurrency level, has the specified initial capacity, and uses the specified IEqualityComparer<T>.

属性

Count Count Count Count

获取包含在 ConcurrentDictionary<TKey,TValue> 中的键/值对的数目。 Gets the number of key/value pairs contained in the ConcurrentDictionary<TKey,TValue>.

IsEmpty IsEmpty IsEmpty IsEmpty

获取一个值,该值指示 ConcurrentDictionary<TKey,TValue> 是否为空。 Gets a value that indicates whether the ConcurrentDictionary<TKey,TValue> is empty.

Item[TKey] Item[TKey] Item[TKey] Item[TKey]

获取或设置与指定的键关联的值。 Gets or sets the value associated with the specified key.

Keys Keys Keys Keys

获得一个包含 Dictionary<TKey,TValue> 中的键的集合。 Gets a collection containing the keys in the Dictionary<TKey,TValue>.

Values Values Values Values

获取包含 Dictionary<TKey,TValue> 中的值的集合。 Gets a collection that contains the values in the Dictionary<TKey,TValue>.

方法

AddOrUpdate(TKey, Func<TKey,TValue>, Func<TKey,TValue,TValue>) AddOrUpdate(TKey, Func<TKey,TValue>, Func<TKey,TValue,TValue>) AddOrUpdate(TKey, Func<TKey,TValue>, Func<TKey,TValue,TValue>) AddOrUpdate(TKey, Func<TKey,TValue>, Func<TKey,TValue,TValue>)

如果该键不存在,则使用指定函数将键/值对添加到 ConcurrentDictionary<TKey,TValue>;如果该键已存在,则使用该函数更新 ConcurrentDictionary<TKey,TValue> 中的键/值对。 Uses the specified functions to add a key/value pair to the ConcurrentDictionary<TKey,TValue> if the key does not already exist, or to update a key/value pair in the ConcurrentDictionary<TKey,TValue> if the key already exists.

AddOrUpdate(TKey, TValue, Func<TKey,TValue,TValue>) AddOrUpdate(TKey, TValue, Func<TKey,TValue,TValue>) AddOrUpdate(TKey, TValue, Func<TKey,TValue,TValue>) AddOrUpdate(TKey, TValue, Func<TKey,TValue,TValue>)

如果该键不存在,则将键/值对添加到 ConcurrentDictionary<TKey,TValue> 中;如果该键已经存在,则通过使用指定的函数更新 ConcurrentDictionary<TKey,TValue> 中的键/值对。 Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> if the key does not already exist, or updates a key/value pair in the ConcurrentDictionary<TKey,TValue> by using the specified function if the key already exists.

AddOrUpdate<TArg>(TKey, Func<TKey,TArg,TValue>, Func<TKey,TValue,TArg,TValue>, TArg) AddOrUpdate<TArg>(TKey, Func<TKey,TArg,TValue>, Func<TKey,TValue,TArg,TValue>, TArg) AddOrUpdate<TArg>(TKey, Func<TKey,TArg,TValue>, Func<TKey,TValue,TArg,TValue>, TArg) AddOrUpdate<TArg>(TKey, Func<TKey,TArg,TValue>, Func<TKey,TValue,TArg,TValue>, TArg)

使用指定函数和参数将键/值对添加到 ConcurrentDictionary<TKey,TValue>中(若尚无键);或使用指定函数和参数更新 ConcurrentDictionary<TKey,TValue> 中的键/值对(若已有键)。 Uses the specified functions and argument to add a key/value pair to the ConcurrentDictionary<TKey,TValue> if the key does not already exist, or to update a key/value pair in the ConcurrentDictionary<TKey,TValue> if the key already exists.

Clear() Clear() Clear() Clear()

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

ContainsKey(TKey) ContainsKey(TKey) ContainsKey(TKey) ContainsKey(TKey)

确定是否 ConcurrentDictionary<TKey,TValue> 包含指定键。 Determines whether the ConcurrentDictionary<TKey,TValue> contains the specified key.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。 Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

返回循环访问 ConcurrentDictionary<TKey,TValue> 的枚举数。 Returns an enumerator that iterates through the ConcurrentDictionary<TKey,TValue>.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。 Serves as the default hash function.

(Inherited from Object)
GetOrAdd(TKey, Func<TKey,TValue>) GetOrAdd(TKey, Func<TKey,TValue>) GetOrAdd(TKey, Func<TKey,TValue>) GetOrAdd(TKey, Func<TKey,TValue>)

如果该键不存在,则使用指定的函数将键/值对添加到 ConcurrentDictionary<TKey,TValue> 中;如果该键存在,则返回现有值。 Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> by using the specified function if the key does not already exist, or returns the existing value if the key exists.

GetOrAdd(TKey, TValue) GetOrAdd(TKey, TValue) GetOrAdd(TKey, TValue) GetOrAdd(TKey, TValue)

将键/值对添加到 ConcurrentDictionary<TKey,TValue> 中(若尚无键);或返回现有值(若已有键)。 Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> if the key does not already exist, or returns the existing value if the key exists.

GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg) GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg) GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg) GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg)

使用指定函数和参数将键/值对添加到 ConcurrentDictionary<TKey,TValue>中(若尚无键);或返回现有值(若已有键)。 Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> by using the specified function and an argument if the key does not already exist, or returns the existing value if the key exists.

GetType() GetType() GetType() GetType()

获取当前实例的 Type Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。 Creates a shallow copy of the current Object.

(Inherited from Object)
ToArray() ToArray() ToArray() ToArray()

ConcurrentDictionary<TKey,TValue> 中存储的键和值对复制到新数组中。 Copies the key and value pairs stored in the ConcurrentDictionary<TKey,TValue> to a new array.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。 Returns a string that represents the current object.

(Inherited from Object)
TryAdd(TKey, TValue) TryAdd(TKey, TValue) TryAdd(TKey, TValue) TryAdd(TKey, TValue)

尝试将指定的键和值添加到 ConcurrentDictionary<TKey,TValue> 中。 Attempts to add the specified key and value to the ConcurrentDictionary<TKey,TValue>.

TryGetValue(TKey, TValue) TryGetValue(TKey, TValue) TryGetValue(TKey, TValue) TryGetValue(TKey, TValue)

尝试从 ConcurrentDictionary<TKey,TValue> 获取与指定的键关联的值。 Attempts to get the value associated with the specified key from the ConcurrentDictionary<TKey,TValue>.

TryRemove(TKey, TValue) TryRemove(TKey, TValue) TryRemove(TKey, TValue) TryRemove(TKey, TValue)

尝试从 ConcurrentDictionary<TKey,TValue> 中移除并返回具有指定键的值。 Attempts to remove and return the value that has the specified key from the ConcurrentDictionary<TKey,TValue>.

TryUpdate(TKey, TValue, TValue) TryUpdate(TKey, TValue, TValue) TryUpdate(TKey, TValue, TValue) TryUpdate(TKey, TValue, TValue)

如果具有 key 的现有值等于 comparisonValue,则将与 key 关联的值更新为 newValue Updates the value associated with key to newValue if the existing value with key is equal to comparisonValue.

显式界面实现

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

从特定的数组索引处开始,将 ICollection 的元素复制到一个数组中。 Copies the elements of the ICollection to an array, starting at the specified array index.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

获取一个指示对 ICollection 的访问是否与 SyncRoot 同步的值。 Gets a value indicating whether access to the ICollection is synchronized with the SyncRoot.

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

获取可用于同步对 ICollection 的访问的对象。 Gets an object that can be used to synchronize access to the ICollection. 不支持此属性。 This property is not supported.

ICollection<KeyValuePair<TKey,TValue>>.Add(KeyValuePair<TKey,TValue>) ICollection<KeyValuePair<TKey,TValue>>.Add(KeyValuePair<TKey,TValue>) ICollection<KeyValuePair<TKey,TValue>>.Add(KeyValuePair<TKey,TValue>) ICollection<KeyValuePair<TKey,TValue>>.Add(KeyValuePair<TKey,TValue>)
ICollection<KeyValuePair<TKey,TValue>>.Contains(KeyValuePair<TKey,TValue>) ICollection<KeyValuePair<TKey,TValue>>.Contains(KeyValuePair<TKey,TValue>) ICollection<KeyValuePair<TKey,TValue>>.Contains(KeyValuePair<TKey,TValue>) ICollection<KeyValuePair<TKey,TValue>>.Contains(KeyValuePair<TKey,TValue>)
ICollection<KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair<TKey,TValue>[], Int32) ICollection<KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair<TKey,TValue>[], Int32) ICollection<KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair<TKey,TValue>[], Int32) ICollection<KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair<TKey,TValue>[], Int32)
ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly
ICollection<KeyValuePair<TKey,TValue>>.Remove(KeyValuePair<TKey,TValue>) ICollection<KeyValuePair<TKey,TValue>>.Remove(KeyValuePair<TKey,TValue>) ICollection<KeyValuePair<TKey,TValue>>.Remove(KeyValuePair<TKey,TValue>) ICollection<KeyValuePair<TKey,TValue>>.Remove(KeyValuePair<TKey,TValue>)
IDictionary.Add(Object, Object) IDictionary.Add(Object, Object) IDictionary.Add(Object, Object) IDictionary.Add(Object, Object)

将指定的键和值添加到字典中。 Adds the specified key and value to the dictionary.

IDictionary.Contains(Object) IDictionary.Contains(Object) IDictionary.Contains(Object) IDictionary.Contains(Object)

获取 IDictionary<TKey,TValue> 是否包含具有指定键的元素。 Gets whether the IDictionary<TKey,TValue> contains an element with the specified key.

IDictionary.GetEnumerator() IDictionary.GetEnumerator() IDictionary.GetEnumerator() IDictionary.GetEnumerator()

IDictionaryEnumerator 提供一个 IDictionary<TKey,TValue> Provides a IDictionaryEnumerator for the IDictionary<TKey,TValue>.

IDictionary.IsFixedSize IDictionary.IsFixedSize IDictionary.IsFixedSize IDictionary.IsFixedSize

获取一个值,该值指示 IDictionary<TKey,TValue> 是否具有固定大小。 Gets a value indicating whether the IDictionary<TKey,TValue> has a fixed size.

IDictionary.IsReadOnly IDictionary.IsReadOnly IDictionary.IsReadOnly IDictionary.IsReadOnly

获取一个值,该值指示 IDictionary<TKey,TValue> 是否为只读。 Gets a value indicating whether the IDictionary<TKey,TValue> is read-only.

IDictionary.Item[Object] IDictionary.Item[Object] IDictionary.Item[Object] IDictionary.Item[Object]

获取或设置与指定的键关联的值。 Gets or sets the value associated with the specified key.

IDictionary.Keys IDictionary.Keys IDictionary.Keys IDictionary.Keys

获取包含 ICollection 的键的 IDictionary<TKey,TValue> Gets an ICollection that contains the keys of the IDictionary<TKey,TValue>.

IDictionary.Remove(Object) IDictionary.Remove(Object) IDictionary.Remove(Object) IDictionary.Remove(Object)

IDictionary 中移除带有指定键的元素。 Removes the element with the specified key from the IDictionary.

IDictionary.Values IDictionary.Values IDictionary.Values IDictionary.Values

获取包含 ICollection 中的值的 IDictionary Gets an ICollection that contains the values in the IDictionary.

IDictionary<TKey,TValue>.Add(TKey, TValue) IDictionary<TKey,TValue>.Add(TKey, TValue) IDictionary<TKey,TValue>.Add(TKey, TValue) IDictionary<TKey,TValue>.Add(TKey, TValue)
IDictionary<TKey,TValue>.Remove(TKey) IDictionary<TKey,TValue>.Remove(TKey) IDictionary<TKey,TValue>.Remove(TKey) IDictionary<TKey,TValue>.Remove(TKey)
IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

返回循环访问 ConcurrentDictionary<TKey,TValue> 的枚举数。 Returns an enumerator that iterates through the ConcurrentDictionary<TKey,TValue>.

IReadOnlyDictionary<TKey,TValue>.Keys IReadOnlyDictionary<TKey,TValue>.Keys IReadOnlyDictionary<TKey,TValue>.Keys IReadOnlyDictionary<TKey,TValue>.Keys
IReadOnlyDictionary<TKey,TValue>.Values IReadOnlyDictionary<TKey,TValue>.Values IReadOnlyDictionary<TKey,TValue>.Values IReadOnlyDictionary<TKey,TValue>.Values

适用于

线程安全性

所有公共和受保护成员ConcurrentDictionary<TKey,TValue>是线程安全的可能从多个线程同时使用。 All public and protected members of ConcurrentDictionary<TKey,TValue> are thread-safe and may be used concurrently from multiple threads. 但是,通过一个接口访问成员ConcurrentDictionary<TKey,TValue>实现,其中包括扩展方法不能保证是线程安全并可能需要由调用方同步。 However, members accessed through one of the interfaces the ConcurrentDictionary<TKey,TValue> implements, including extension methods, are not guaranteed to be thread safe and may need to be synchronized by the caller.

另请参阅