JD2022-TU1/main/tools/framework/JD.Collections/Extensions/KeyedCollectionExtensions.cs

34 lines
No EOL
1.2 KiB
C#

using JD.Collections.Interfaces;
namespace JD.Collections.Extensions
{
/// <summary>
/// Adds behavior on KeyedCollection based on features of the interface.
/// </summary>
public static class KeyedCollectionExtensions
{
/// <summary>
/// Returns the value corresponding to the given key in the dictionary or the default value if the key is not in the dictionary.
/// </summary>
public static TValue ValueOrDefault<TKey, TValue>(this IKeyedCollection<TKey, TValue> collection, TKey key)
{
TValue result;
collection.TryGetValue(key, out result);
return result;
}
/// <summary>
/// Returns the value corresponding to the given key in the dictionary or the default value if the key is not in the dictionary.
/// </summary>
public static bool TryGetValue<TKey, TValue>(this System.Collections.ObjectModel.KeyedCollection<TKey, TValue> collection, TKey key, out TValue value)
{
if (collection.Contains(key))
{
value = collection[key];
return true;
}
value = default(TValue);
return false;
}
}
}