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)]
[System.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>
属性
实现

示例

下面的示例演示如何构造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>的对象,可以通过在运行时环境中<gcAllowVeryLargeObjects>将配置元素设置为来true将64位系统上的最大数组大小增加到 2 gb。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 <gcAllowVeryLargeObjects> configuration element to true in the run-time environment.

备注

ConcurrentDictionary<TKey,TValue>.NET Framework 4.6.NET Framework 4.6 ConcurrentDictionary<TKey,TValue>实现和接口IReadOnlyDictionary<TKey,TValue> (从开始); 在以前版本的 .NET Framework 中,类未实现这些接口。 IReadOnlyCollection<T>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.

与类一样, ConcurrentDictionary<TKey,TValue>实现IDictionary<TKey,TValue>接口。 System.Collections.Generic.Dictionary<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 索引器的 setter: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. 唯一的例外是接受委托AddOrUpdate的方法,即和。 GetOrAddThe 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. 如果该键存在,则返回新值或现有值。Returns the new value, or 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. 如果该键存在,则返回新值或现有值。Returns the new value, or 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()

获取当前实例的 TypeGets 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 关联的值更新为 newValueUpdates 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 that indicates 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>)

向集合中添加一项。Adds an item to the collection.

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<T> 是否包含具有指定键的元素。Gets whether the ICollection<T> contains an element with the specified key.

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 中的元素复制到一个数组中。Copies the elements of the ICollection to an array, starting at the specified array index.

ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly

获取一个值,该值指示 ICollection 是否为只读。Gets a value that indicates whether the ICollection is read-only.

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>)

将指定的键/值对从集合中移除。Removes the specified key/value pair from the collection.

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 a value that indicates 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 that indicates whether the IDictionary<TKey,TValue> has a fixed size.

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

获取一个值,该值指示 IDictionary<TKey,TValue> 是否为只读。Gets a value that indicates 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 中的值的 IDictionaryGets 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> 中添加指定的键和值。Adds the specified key and value to the IDictionary<TKey,TValue>.

IDictionary<TKey,TValue>.Remove(TKey) IDictionary<TKey,TValue>.Remove(TKey) IDictionary<TKey,TValue>.Remove(TKey) IDictionary<TKey,TValue>.Remove(TKey)

IDictionary<TKey,TValue> 中移除包含指定键的元素。Removes the element with the specified key from the IDictionary<TKey,TValue>.

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

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

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

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

扩展方法

GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey) GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey) GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey) GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey)
GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey, TValue) GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey, TValue) GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey, TValue) GetValueOrDefault<TKey,TValue>(IReadOnlyDictionary<TKey,TValue>, TKey, TValue)
Remove<TKey,TValue>(IDictionary<TKey,TValue>, TKey, TValue) Remove<TKey,TValue>(IDictionary<TKey,TValue>, TKey, TValue) Remove<TKey,TValue>(IDictionary<TKey,TValue>, TKey, TValue) Remove<TKey,TValue>(IDictionary<TKey,TValue>, TKey, TValue)
TryAdd<TKey,TValue>(IDictionary<TKey,TValue>, TKey, TValue) TryAdd<TKey,TValue>(IDictionary<TKey,TValue>, TKey, TValue) TryAdd<TKey,TValue>(IDictionary<TKey,TValue>, TKey, TValue) TryAdd<TKey,TValue>(IDictionary<TKey,TValue>, TKey, TValue)
CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>)

在给定其泛型参数 TDataTable 的输入 DataRow 对象的情况下,返回包含 IEnumerable<T> 对象副本的 DataRowReturns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

Ancestors<T>(IEnumerable<T>) Ancestors<T>(IEnumerable<T>) Ancestors<T>(IEnumerable<T>) Ancestors<T>(IEnumerable<T>)

返回元素集合,其中包含源集合中每个节点的上级。Returns a collection of elements that contains the ancestors of every node in the source collection.

Ancestors<T>(IEnumerable<T>, XName) Ancestors<T>(IEnumerable<T>, XName) Ancestors<T>(IEnumerable<T>, XName) Ancestors<T>(IEnumerable<T>, XName)

返回经过筛选的元素集合,其中包含源集合中每个节点的上级。Returns a filtered collection of elements that contains the ancestors of every node in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

DescendantNodes<T>(IEnumerable<T>) DescendantNodes<T>(IEnumerable<T>) DescendantNodes<T>(IEnumerable<T>) DescendantNodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子代节点的集合。Returns a collection of the descendant nodes of every document and element in the source collection.

Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>)

返回元素集合,其中包含源集合中每个元素和文档的子代元素。Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

Descendants<T>(IEnumerable<T>, XName) Descendants<T>(IEnumerable<T>, XName) Descendants<T>(IEnumerable<T>, XName) Descendants<T>(IEnumerable<T>, XName)

返回经过筛选的元素集合,其中包含源集合中每个元素和文档的子代元素。Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

Elements<T>(IEnumerable<T>) Elements<T>(IEnumerable<T>) Elements<T>(IEnumerable<T>) Elements<T>(IEnumerable<T>)

返回源集合中每个元素和文档的子元素的集合。Returns a collection of the child elements of every element and document in the source collection.

Elements<T>(IEnumerable<T>, XName) Elements<T>(IEnumerable<T>, XName) Elements<T>(IEnumerable<T>, XName) Elements<T>(IEnumerable<T>, XName)

返回源集合中经过筛选的每个元素和文档的子元素集合。Returns a filtered collection of the child elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

InDocumentOrder<T>(IEnumerable<T>) InDocumentOrder<T>(IEnumerable<T>) InDocumentOrder<T>(IEnumerable<T>) InDocumentOrder<T>(IEnumerable<T>)

返回节点集合(其中包含源集合中的所有节点),并按文档顺序排列。Returns a collection of nodes that contains all nodes in the source collection, sorted in document order.

Nodes<T>(IEnumerable<T>) Nodes<T>(IEnumerable<T>) Nodes<T>(IEnumerable<T>) Nodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子节点集合。Returns a collection of the child nodes of every document and element in the source collection.

Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>)

将源集合中的每个节点从其父节点中移除。Removes every node in the source collection from its parent node.

适用于

线程安全性

的所有公共和受保护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.

另请参阅