Class IDictionaryExtensions
IDictionary 扩展方法
Inherited Members
Namespace: Lett.Extensions
Assembly: Lett.Extensions.dll
Syntax
public static class IDictionaryExtensions
Methods
| Improve this Doc View SourceAddOrUpdate<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)
添加或更新
当 this
中不存在 key
时,将 {key
, value
} 添加到 this
中
Declaration
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> this, TKey key, TValue value)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
TKey | key | 键 |
TValue | value | 值 |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
Examples
var dict2 = new Dictionary<string, int>{{"a",1},{"b",2}};
dict2.AddOrSet(new KeyValuePair<string, int>("a",3));
dict2.AddOrSet(new KeyValuePair<string, int>("c",3));
// dict1 => {{"a", 3}, {"b", 2}, {"c", 3}}
Exceptions
Type | Condition |
---|---|
ArgumentNullException | key is null |
NotSupportedException |
|
AddOrUpdate<TKey, TValue>(IDictionary<TKey, TValue>, KeyValuePair<TKey, TValue>)
添加或更新
当 this
中不存在 pair
的 Key
时,将 pair
的 Value
添加到 this
中
Declaration
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> this, KeyValuePair<TKey, TValue> pair)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
KeyValuePair<TKey, TValue> | pair | 键值对 |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
Examples
var dict1 = new Dictionary<string, int> {{"a", 1}, {"b", 2}};
dict1.AddOrSet("a", 3);
dict1.AddOrSet("c", 3);
// dict1 => {{"a", 3}, {"b", 2}, {"c", 3}}
Exceptions
Type | Condition |
---|---|
ArgumentNullException | key is null |
NotSupportedException |
|
AddOrUpdateRange<TKey, TValue>(IDictionary<TKey, TValue>, IEnumerable<KeyValuePair<TKey, TValue>>)
批量添加或更新
遍历pairs
, 当 this
中不存在 pair
的 Key
时,将 pair
的 Value
添加到 this
中
Declaration
public static void AddOrUpdateRange<TKey, TValue>(this IDictionary<TKey, TValue> this, IEnumerable<KeyValuePair<TKey, TValue>> pairs)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
IEnumerable<KeyValuePair<TKey, TValue>> | pairs |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
Examples
var dict1 = new Dictionary<string, int> {{"a", 1}, {"b", 2}};
var sameDict = new Dictionary<string, int> {{"a", 3}, {"b", 4}, {"c", 5}};
dict1.AddOrUpdateRange(sameDict);
// dict1 => {{"a", 3}, {"b", 4}, {"c", 5}}
Exceptions
Type | Condition |
---|---|
ArgumentNullException | |
NotSupportedException |
|
AddRange<TKey, TValue>(IDictionary<TKey, TValue>, IEnumerable<KeyValuePair<TKey, TValue>>)
批量添加
Declaration
public static void AddRange<TKey, TValue>(this IDictionary<TKey, TValue> this, IEnumerable<KeyValuePair<TKey, TValue>> pairs)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
IEnumerable<KeyValuePair<TKey, TValue>> | pairs | 键值对集合 |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
Examples
var dict = new Dictionary<string, int> {{"a", 1}, {"b", 2}};
var appendDict = new Dictionary<string, int> {{"c", 3}, {"d", 4}};
dict.AddRange(appendDict);
// dict = {{"a", 1}, {"b", 2}, {"c", 3}, {"d", 4}};
Exceptions
Type | Condition |
---|---|
ArgumentException |
|
AddRangeParams<TKey, TValue>(IDictionary<TKey, TValue>, KeyValuePair<TKey, TValue>[])
批量添加
Declaration
public static void AddRangeParams<TKey, TValue>(this IDictionary<TKey, TValue> this, params KeyValuePair<TKey, TValue>[] pairs)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
KeyValuePair<TKey, TValue>[] | pairs | 键值对数组 |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
Examples
var dict = new Dictionary<string, int> {{"a", 1}, {"b", 2}};
dict.AddRangeParams(new KeyValuePair<string, int>("c", 3), new KeyValuePair<string, int>("d", 4));
// dict = {{"a", 1}, {"b", 2}, {"c", 3}, {"d", 4}};
Exceptions
Type | Condition |
---|---|
ArgumentException |
|
ContainsKeyAll<TKey, TValue>(IDictionary<TKey, TValue>, IEnumerable<TKey>)
是否包含全部 Key
Declaration
public static bool ContainsKeyAll<TKey, TValue>(this IDictionary<TKey, TValue> this, IEnumerable<TKey> keys)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
IEnumerable<TKey> | keys | Key 集合 |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
TKey | Key 类型 |
TValue | Value 类型 |
var dict = new Dictionary<string, object>
{
{"1", "1"}, {"2", "2"}, {"3", "3"}, {"4", "4"},
};
var keys = new[] {"6"};
var keys2 = new List<string> {"1", "2", "3", "4"};
dict.ContainsKeyAll(keys); // false
dict.ContainsKeyAll(keys2); // true
var dict = new Dictionary<string, object>
{
{"1", "1"}, {"2", "2"}, {"3", "3"}, {"4", "4"},
};
Dictionary<string, object> dict2 = null;
var keys = new[] {"6"};
List<string> keys3 = null;
dict2.ContainsKeyAll(keys); // error -> throw ArgumentNullException dict2 is null
dict.ContainsKeyAll(keys3); // error -> throw ArgumentNullException keys3 is null
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ContainsKeyAllParams<TKey, TValue>(IDictionary<TKey, TValue>, TKey[])
是否包含全部 Key
Declaration
public static bool ContainsKeyAllParams<TKey, TValue>(this IDictionary<TKey, TValue> this, params TKey[] keys)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
TKey[] | keys | params |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
TKey | Key 类型 |
TValue | Value 类型 |
Examples
var dict = new Dictionary<string, object>
{
{"1", "1"}, {"2", "2"}, {"3", "3"}, {"4", "4"},
};
dict.ContainsKeyAll(keys3); // error -> throw ArgumentNullException keys3 is null
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ContainsKeyAny<TKey, TValue>(IDictionary<TKey, TValue>, IEnumerable<TKey>)
是否包含任一 Key
Declaration
public static bool ContainsKeyAny<TKey, TValue>(this IDictionary<TKey, TValue> this, IEnumerable<TKey> keys)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
IEnumerable<TKey> | keys | Key 集合 |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
TKey | Key 类型 |
TValue | Value 类型 |
Examples
var dict = new Dictionary<string, object>
{
{"1", "1"}, {"2", "2"}, {"3", "3"}, {"4", "4"},
};
var keys = new[] {"6"};
var keys2 = new List<string> {"1"};
dict.ContainsKeyAny(keys); // false
dict.ContainsKeyAny(keys2); // true
var dict = new Dictionary<string, object>
{
{"1", "1"}, {"2", "2"}, {"3", "3"}, {"4", "4"},
};
Dictionary<string, object> dict2 = null;
var keys = new[] {"6"};
List<string> keys3 = null;
dict2.ContainsKeyAny(keys); // error -> throw ArgumentNullException dict2 is null
dict.ContainsKeyAny(keys3); // error -> throw ArgumentNullException keys3 is null
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ContainsKeyAnyParams<TKey, TValue>(IDictionary<TKey, TValue>, TKey[])
是否包含任一 Key
Declaration
public static bool ContainsKeyAnyParams<TKey, TValue>(this IDictionary<TKey, TValue> this, params TKey[] keys)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
TKey[] | keys | params |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
TKey | Key 类型 |
TValue | Value 类型 |
Examples
var dict = new Dictionary<string, object>
{
{"1", "1"}, {"2", "2"}, {"3", "3"}, {"4", "4"},
};
dict2.ContainsKeyAny("1", "2"); // true
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ContainsValue<TKey, TValue>(IDictionary<TKey, TValue>, TValue)
是否包含 value
Declaration
public static bool ContainsValue<TKey, TValue>(this IDictionary<TKey, TValue> this, TValue value)
where TValue : class
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
TValue | value |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue | 泛型约束: class |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ContainsValueAll<TKey, TValue>(IDictionary<TKey, TValue>, IEnumerable<TValue>)
是否包含全部 Value
Declaration
public static bool ContainsValueAll<TKey, TValue>(this IDictionary<TKey, TValue> this, IEnumerable<TValue> values)
where TValue : class
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
IEnumerable<TValue> | values |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
TKey | Key 类型 |
TValue | Value 类型 泛型约束: class |
Examples
var dict = new Dictionary<string, object>
{
{"Key1", "1Value"}, {"Key2", "2Value"}, {"Key3", "3Value"}, {"Key4", "4Value"},
};
var values = new List<string> {"1Value", "2Value", "3Value", "4Value"};
var values2 = new List<string> {"1Value", "2Value", "3Value", "4Value", "6666Value"};
Dictionary<string, object> dict2 = null;
dict.ContainsValueAll(values); // true
dict.ContainsValueAll(values2); // false
dict.ContainsValueAllParams("1Value", "2Value", "3Value", "4Value"); // true
dict.ContainsValueAllParams("1Value", "2Value", "3Value", "4Value","6666Value"); // false
dict2.ContainsValueAll(values); // throw ArgumentNullException this is null
dict.ContainsValueAllParams(null); // throw ArgumentNullException values is null
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ContainsValueAllParams<TKey, TValue>(IDictionary<TKey, TValue>, TValue[])
是否包含全部 Value
Declaration
public static bool ContainsValueAllParams<TKey, TValue>(this IDictionary<TKey, TValue> this, params TValue[] values)
where TValue : class
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
TValue[] | values | params |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
TKey | Key 类型 |
TValue | Value 类型 泛型约束: class |
Examples
var dict = new Dictionary<string, object>
{
{"Key1", "1Value"}, {"Key2", "2Value"}, {"Key3", "3Value"}, {"Key4", "4Value"},
};
var values = new List<string> {"1Value", "2Value", "3Value", "4Value"};
var values2 = new List<string> {"1Value", "2Value", "3Value", "4Value", "6666Value"};
Dictionary<string, object> dict2 = null;
dict.ContainsValueAll(values); // true
dict.ContainsValueAll(values2); // false
dict.ContainsValueAllParams("1Value", "2Value", "3Value", "4Value"); // true
dict.ContainsValueAllParams("1Value", "2Value", "3Value", "4Value","6666Value"); // false
dict2.ContainsValueAll(values); // throw ArgumentNullException this is null
dict.ContainsValueAllParams(null); // throw ArgumentNullException values is null
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ContainsValueAny<TKey, TValue>(IDictionary<TKey, TValue>, IEnumerable<TValue>)
是否包含任一 value
Declaration
public static bool ContainsValueAny<TKey, TValue>(this IDictionary<TKey, TValue> this, IEnumerable<TValue> values)
where TValue : class
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
IEnumerable<TValue> | values | value 集合 |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
TKey | Key 类型 |
TValue | Value 类型 泛型约束: class |
Examples
var dict = new Dictionary<string,object>
{
{"Key1", "1Value"}, {"Key2", "2Value"}, {"Key3", "3Value"}, {"Key4", "4Value"},
};
var values = new List<string> {"1Value"};
var values2 = new List<string> {"6666Value"};
Dictionary<string, object> dict2 = null;
dict.ContainsValueAny(values); // true
dict.ContainsValueAny(values2); // false
dict.ContainsValueAnyParams("1Value"); // true
dict2.ContainsKeyAll(values); // throw ArgumentNullException this is null
dict.ContainsKeyAllParams(null); // throw ArgumentNullException values is null
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ContainsValueAnyParams<TKey, TValue>(IDictionary<TKey, TValue>, TValue[])
是否包含任一 value
Declaration
public static bool ContainsValueAnyParams<TKey, TValue>(this IDictionary<TKey, TValue> this, params TValue[] values)
where TValue : class
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
TValue[] | values | params |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
TKey | Key 类型 |
TValue | Value 类型 泛型约束: class |
Examples
var dict = new Dictionary<string,object>
{
{"Key1", "1Value"}, {"Key2", "2Value"}, {"Key3", "3Value"}, {"Key4", "4Value"},
};
var values = new List<string> {"1Value"};
var values2 = new List<string> {"6666Value"};
Dictionary<string, object> dict2 = null;
dict.ContainsValueAny(values); // true
dict.ContainsValueAny(values2); // false
dict.ContainsValueAnyParams("1Value"); // true
dict2.ContainsKeyAll(values); // throw ArgumentNullException this is null
dict.ContainsKeyAllParams(null); // throw ArgumentNullException values is null
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
GetOrUpdate<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)
获取值或更新
当 this
中不存在 key
时,将 {key
, value
} 添加到 this
中,并返回 value
Declaration
public static TValue GetOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> this, TKey key, TValue value)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | this | |
TKey | key | 键 |
TValue | value | 值 |
Returns
Type | Description |
---|---|
TValue |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
Examples
var dict = new Dictionary<string, int> {{"a", 1}, {"b", 2}};
var rs = dict.GetOrUpdate("c", 3);
// rs = 3;
// dict = {{"a", 1}, {"b", 2}, {"c", 3}};
Exceptions
Type | Condition |
---|---|
ArgumentNullException | key is null |
NotSupportedException |
|