I have a method that returns an ILookup
. In some cases I want to return an empty ILookup
as an early exit. What is the best way of constructing an empty ILookup
?
Further to the answers from mquander and Vasile Bujac, you could create a nice, straightforward singleton-esque EmptyLookup<K,E>
class as follows. (In my opinion, there doesn't seem much benefit to creating a full ILookup<K,E>
implementation as per Vasile's answer.)
var empty = EmptyLookup<int, string>.Instance;
// ...
public static class EmptyLookup<TKey, TElement>
{
private static readonly ILookup<TKey, TElement> _instance
= Enumerable.Empty<TElement>().ToLookup(x => default(TKey));
public static ILookup<TKey, TElement> Instance
{
get { return _instance; }
}
}