Skip to content

Class LazyConcurrentDictionary<TKey, TValue>

Namespace: Tool.Utils.Data
Assembly: Tool.Net.dll

完全保证的多线程延迟加载字典,表示可由多个线程同时访问的键/值对的线程安全集合。

csharp
public class LazyConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable

Type Parameters

TKey

字典中的键的类型。

TValue

字典中的值的类型。

Inheritance

objectLazyConcurrentDictionary<TKey, TValue>

Implements

IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable

Inherited Members

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Extension Methods

ObjectExtension.Add<T>(object, object, object), DictionaryExtension.AsReadOnly<TKey, TValue>(IDictionary<TKey, TValue>), ObjectExtension.CopyEntity(object, object, params string[]), ObjectExtension.EntityToJson(object), ObjectExtension.EntityToJson(object, bool), ObjectExtension.EntityToJson(object, bool, string), DictionaryExtension.GetDictionary(object), TypeExtension.GetFieldKey(object, string, out bool), TypeExtension.GetFieldKey<T>(object, string, out bool), TypeExtension.GetFieldKey(object, Type, string, out bool), ObjectExtension.GetIntPtr(object), ObjectExtension.GetIntPtrInt(object), TypeExtension.GetPropertieFind(object, string, bool), TypeExtension.GetProperties(object), TypeExtension.GetPropertyKey(object, string, out bool), TypeExtension.GetPropertyKey<T>(object, string, out bool), TypeExtension.GetPropertyKey(object, Type, string, out bool), TypeExtension.GetValue(object, PropertyDescriptor), TypeExtension.GetValue(object, string, bool), TypeExtension.GetValue(object, string), DictionaryExtension.NewDictionary<TKey, TValue>(IDictionary<TKey, TValue>), ObjectExtension.Read<T>(object, object, int, int), ObjectExtension.Read<T>(object, int, object, int, int), DictionaryExtension.Remove<TKey, TValue>(IDictionary<TKey, TValue>, params TKey[]), DictionaryExtension.SetDictionary(object, IDictionary<string, object>), TypeExtension.SetFieldKey(object, string, object), TypeExtension.SetFieldKey<T>(object, string, object), TypeExtension.SetFieldKey(object, Type, string, object), TypeExtension.SetPropertyKey(object, string, object), TypeExtension.SetPropertyKey<T>(object, string, object), TypeExtension.SetPropertyKey(object, Type, string, object), TypeExtension.SetValue(object, PropertyDescriptor, object), TypeExtension.SetValue(object, string, object, bool), TypeExtension.SetValue(object, string, object), ObjectExtension.ToBase64String(object), ObjectExtension.ToBytes(object), ObjectExtension.ToBytes(object, out Type), DictionaryExtension.ToDictionary(object), DictionaryExtension.ToDictionary<T>(object), DictionaryExtension.ToIDictionary(object), DictionaryExtension.ToIDictionary<T>(object), ObjectExtension.ToJson(object), ObjectExtension.ToJson(object, JsonSerializerOptions), ObjectExtension.ToJsonWeb(object), ObjectExtension.ToJsonWeb(object, Action<JsonSerializerOptions>), ObjectExtension.ToTryVar<T>(object, T), ObjectExtension.ToVar<T>(object), ObjectExtension.ToVar(object, Type, bool), ObjectExtension.ToVar(object, string), ObjectExtension.ToXml(object), DictionaryExtension.TryRemove<TKey, TValue>(IDictionary<TKey, TValue>, out TKey[], params TKey[])

Remarks

代码由逆血提供支持

Constructors

LazyConcurrentDictionary()

初始化

csharp
public LazyConcurrentDictionary()

Properties

Count

获取包含在 中的键/值对的数目。

csharp
public int Count { get; }

Property Value

int

Exceptions

OverflowException

字典已包含最大数目的元素 (System.Int32.MaxValue)。

IsEmpty

获取一个指示 是否为空的值。

csharp
public bool IsEmpty { get; }

Property Value

bool

IsReadOnly

获取一个值,该值指示 是否为只读。(无效参数)

csharp
public bool IsReadOnly { get; }

Property Value

bool

Keys

获取包含 中的键的集合。

csharp
public ICollection<TKey> Keys { get; }

Property Value

ICollection<TKey>

Values

获取包含 中的值的集合。 (作者并不建议使用该字段,开销有点大)

csharp
public ICollection<TValue> Values { get; }

Property Value

ICollection<TValue>

this[TKey]

获取或设置与指定的键相关联的值。

csharp
public TValue this[TKey key] { get; set; }

Property Value

TValue

Exceptions

ArgumentNullException

key 为 null。

KeyNotFoundException

已检索该属性,并且集合中不存在 key。

Methods

Add(TKey, TValue)

中添加一个带有所提供的键和值的元素。

csharp
public void Add(TKey key, TValue value)

Parameters

key TKey

用作要添加的元素的键的对象。

value TValue

作为要添加的元素的值的对象。

Add(KeyValuePair<TKey, TValue>)

中添加一个带有所提供的键和值的元素。

csharp
public void Add(KeyValuePair<TKey, TValue> item)

Parameters

item KeyValuePair<TKey, TValue>

单个对象的键值对

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

使用指定的函数将键/值对添加到 如果密钥已经存在,更新系统中的密钥/值对。如果密钥不存在 则添加进去。

csharp
public TValue AddOrUpdate(TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)

Parameters

key TKey

元素的键

addValue TValue

新增的内容

updateValueFactory Func<TKey, TValue, TValue>

修改的内容委托

Returns

TValue

返回值

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

使用指定的函数将键/值对添加到 如果密钥已经存在,更新系统中的密钥/值对。如果密钥不存在 则添加进去。

csharp
public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)

Parameters

key TKey

元素的键

addValueFactory Func<TKey, TValue>

新增的内容委托

updateValueFactory Func<TKey, TValue, TValue>

修改的内容委托

Returns

TValue

返回值

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

使用指定的函数将键/值对添加到 如果密钥已经存在,更新系统中的密钥/值对。如果密钥不存在 则添加进去。

csharp
public TValue AddOrUpdate<TArg>(TKey key, Func<TKey, TArg, TValue> addValueFactory, Func<TKey, TValue, TArg, TValue> updateValueFactory, TArg factoryArgument)

Parameters

key TKey

元素的键

addValueFactory Func<TKey, TArg, TValue>

新增的内容委托

updateValueFactory Func<TKey, TValue, TArg, TValue>

修改的内容委托

factoryArgument TArg

传入的额外对象

Returns

TValue

返回值

Type Parameters

TArg

Clear()

中移除所有的键和值。

csharp
public void Clear()

ContainsKey(TKey)

确定 是否包含指定的键。

csharp
public bool ContainsKey(TKey key)

Parameters

key TKey

要在 中定位的键。

Returns

bool

如果 包含具有指定键的元素,则为 true;否则为 false。

Exceptions

ArgumentNullException

key 为 null。

GetEnumerator()

返回循环访问 的枚举数。

csharp
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()

Returns

IEnumerator<KeyValuePair<TKey, TValue>>

的一个枚举数。

GetOrAdd(TKey, Func<TKey, TValue>)

如果该键尚不存在,则使用指定函数将键/值对添加到

csharp
public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)

Parameters

key TKey

要添加的元素的键。

valueFactory Func<TKey, TValue>

用于为键生成值的函数

Returns

TValue

键的值。 如果字典中已存在指定的键,则为该键的现有值;如果字典中不存在指定的键,则为 valueFactory 返回的键的新值。

Exceptions

ArgumentNullException

valueFactory 为 null。

OverflowException

字典已包含最大数目的元素 (System.Int32.MaxValue)。

GetOrAdd(TKey, TValue)

如果该键尚不存在,则使用指定函数将键/值对添加到

csharp
public TValue GetOrAdd(TKey key, TValue value)

Parameters

key TKey

要添加的元素的键。

value TValue

用于为键生成值的函数

Returns

TValue

键的值。 如果字典中已存在指定的键,则为该键的现有值;如果字典中不存在指定的键,则为新值。

Exceptions

ArgumentNullException

value 为 null。

OverflowException

字典已包含最大数目的元素 (System.Int32.MaxValue)。

Remove(TKey)

尝试从 中移除。

csharp
public bool Remove(TKey key)

Parameters

key TKey

要移除并返回的元素的键。

Returns

bool

如果已成功移除对象,则为 true;否则为 false。

ToString()

显示结果

csharp
public override string ToString()

Returns

string

TryAdd(TKey, TValue)

中添加一个带有所提供的键和值的元素。

csharp
public bool TryAdd(TKey key, TValue value)

Parameters

key TKey

用作要添加的元素的键的对象。

value TValue

作为要添加的元素的值的对象。

Returns

bool

如果该键/值对已成功添加到 ,则为 true;如果该键已存在,则为 false。

Exceptions

ArgumentNullException

value 为 null。

OverflowException

字典已包含最大数目的元素 (System.Int32.MaxValue)。

TryAdd(TKey, Func<TKey, TValue>)

中添加一个带有所提供的键和值的元素。

csharp
public bool TryAdd(TKey key, Func<TKey, TValue> addValueFactory)

Parameters

key TKey

用作要添加的元素的键的对象。

addValueFactory Func<TKey, TValue>

作为要添加的元素的值的委托。

Returns

bool

如果该键/值对已成功添加到 ,则为 true;如果该键已存在,则为 false。

Exceptions

ArgumentNullException

value 为 null。

OverflowException

字典已包含最大数目的元素 (System.Int32.MaxValue)。

TryGetValue(TKey, out TValue)

尝试从 获取与指定的键关联的值。

csharp
public bool TryGetValue(TKey key, out TValue value)

Parameters

key TKey

要获取的值的键。

value TValue

当此方法返回时,将包含 中具有指定键的对象;如果操作失败,则包含默认值。

Returns

bool

如果在 System.Collections.Concurrent.ConcurrentDictionary`2 中找到该键,则为 true;否则为 false。

Exceptions

ArgumentNullException

key 为 null。

TryRemove(TKey, out TValue)

尝试从 中移除。

csharp
public bool TryRemove(TKey key, out TValue value)

Parameters

key TKey

要移除并返回的元素的键。

value TValue

当此方法返回时,将包含从 中移除的对象;如果 key 不存在,则包含 TValue 类型。

Returns

bool

如果已成功移除对象,则为 true;否则为 false。

TryUpdate(TKey, TValue, TValue)

尝试从 中修改数据(对比数据不一致时进行修改)

csharp
public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue)

Parameters

key TKey

要修改的元素的键

newValue TValue

修改的内容

comparisonValue TValue

比较的内容

Returns

bool

如果已成功修改对象,则为 true;否则为 false。

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

尝试从 中修改数据(对比数据不一致时进行修改)

csharp
public bool TryUpdate(TKey key, Func<TKey, TValue> addValueFactory, TValue comparisonValue)

Parameters

key TKey

要修改的元素的键

addValueFactory Func<TKey, TValue>

修改的内容委托

comparisonValue TValue

比较的内容

Returns

bool

如果已成功修改对象,则为 true;否则为 false。

基于Apache-2.0协议开源