I'm creating an extension method for MultiValueDictionary to encapsulate frequent ContainsKey
checks and I was wondering what was the best way to create an empty IReadOnlyCollection
?.
What I've used so far is new List<TValue>(0).AsReadOnly()
but there must be a better way, an equivilant to IEnumerable
's Enumerable.Empty
public static IReadOnlyCollection<TValue> GetValuesOrEmpty<TKey, TValue>(this MultiValueDictionary<TKey, TValue> multiValueDictionary, TKey key)
{
IReadOnlyCollection<TValue> values;
return !multiValueDictionary.TryGetValue(key, out values) ? new List<TValue>(0).AsReadOnly() : values;
}