表示可由多个线程同时访问的键/值对的线程安全集合。
命名空间: System.Collections.Concurrent
程序集: mscorlib(位于 mscorlib.dll)
1 2 3 4 5 6 7 8 |
[SerializableAttribute] [ComVisibleAttribute(false)] [HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)] public class ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IDictionary, ICollection, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>> |
名称 | 说明 | |
---|---|---|
Count |
获取包含在 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 中的键/值对的数目。 |
|
IsEmpty |
获取一个值,该值指示 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 是否为空。 |
|
Item[TKey] |
获取或设置与指定的键关联的值。 |
|
Keys |
获得一个包含 System.Collections.Generic.Dictionary<TKey, TValue> 中的键的集合。 |
|
Values |
获取包含 System.Collections.Generic.Dictionary<TKey, TValue> 中的值的集合。 |
名称 | 说明 | |
---|---|---|
AddOrUpdate(TKey, TValue, Func<TKey, TValue, TValue>) |
如果该键不存在,则将键/值对添加到 ConcurrentDictionary<TKey, TValue> 中;如果该键已经存在,则通过使用指定的函数更新 ConcurrentDictionary<TKey, TValue> 中的键/值对。 |
|
AddOrUpdate(TKey, Func<TKey, TValue>, Func<TKey, TValue, TValue>) |
如果该键不存在,则使用指定函数将键/值对添加到 ConcurrentDictionary<TKey, TValue>;如果该键已存在,则使用该函数更新 ConcurrentDictionary<TKey, TValue>中的键/值对。 |
|
Clear() |
将所有键和值从 ConcurrentDictionary<TKey, TValue> 中移除。 |
|
ContainsKey(TKey) |
确定是否 ConcurrentDictionary<TKey, TValue> 包含指定键。 |
|
Equals(Object) |
确定指定的对象是否等于当前对象。(继承自 Object。) |
|
Finalize() |
在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。(继承自 Object。) |
|
GetEnumerator() |
返回循环访问 ConcurrentDictionary<TKey, TValue> 的枚举数。 |
|
GetHashCode() |
作为默认哈希函数。(继承自 Object。) |
|
GetOrAdd(TKey, TValue) |
如果该键不存在,则将键/值对添加到 ConcurrentDictionary<TKey, TValue> 中。 |
|
GetOrAdd(TKey, Func<TKey, TValue>) |
如果该键不存在,则通过使用指定的函数将键/值对添加到 ConcurrentDictionary<TKey, TValue> 中。 |
|
GetType() | ||
MemberwiseClone() | ||
ToArray() |
将 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 中存储的键和值对复制到新数组中。 |
|
ToString() |
返回表示当前对象的字符串。(继承自 Object。) |
|
TryAdd(TKey, TValue) |
尝试将指定的键和值添加到 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 中。 |
|
TryGetValue(TKey, TValue) |
尝试从 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 获取与指定的键关联的值。 |
|
TryRemove(TKey, TValue) |
尝试从 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 中移除并返回具有指定键的值。 |
|
TryUpdate(TKey, TValue, TValue) |
将指定键的现有值与指定值进行比较,如果相等,则用第三个值更新该键。 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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); } } |
from: https://msdn.microsoft.com/zh-cn/library/dd287191.aspx