24 lines
976 B
C#
24 lines
976 B
C#
using System.Collections.Generic;
|
|
|
|
namespace Plane.Collections
|
|
{
|
|
public static class DictionaryExtensions
|
|
{
|
|
/// <summary>
|
|
/// 尝试返回字典中与指定的键对应的值,若此键不存在则返回指定的默认值。
|
|
/// </summary>
|
|
/// <typeparam name="TKey">键的类型。</typeparam>
|
|
/// <typeparam name="TValue">值的类型。</typeparam>
|
|
/// <param name="dictionary">源字典。</param>
|
|
/// <param name="key">键。</param>
|
|
/// <param name="defaultValue">默认值。</param>
|
|
/// <returns>若字典中包含指定的键,返回其对应的值;否则返回指定的默认值。</returns>
|
|
public static TValue GetValue<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue = default(TValue))
|
|
{
|
|
var value = defaultValue;
|
|
dictionary.TryGetValue(key, out value);
|
|
return value;
|
|
}
|
|
}
|
|
}
|