[英]Empty ILookup<K, T>
我有一個返回ILookup
的方法。 在某些情況下,我想返回一個空的ILookup
作為提前退出。 構建空的ILookup
的最佳方法是什么?
除了mquander和Vasile Bujac的答案之外,您還可以創建一個漂亮、直接的單例式EmptyLookup<K,E>
類,如下所示。 (在我看來,根據 Vasile 的回答創建完整的ILookup<K,E>
實現似乎沒有太大好處。)
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; }
}
}
沒有內置的,所以我只寫了一個擴展方法,它沿着new T[0].ToLookup<K, T>(x => default(K));
我強烈懷疑在這里返回 null 會更正確。 您想從返回集合的方法(而不是空集合)中返回 null 的情況幾乎從來沒有。我不可能更不同意那些提出這一建議的人。
您可以為空查找創建一個單例類。
using System.Linq;
public sealed class EmptyLookup<T, K> : ILookup<T, K>
{
public static readonly EmptyLookup<T, K> Instance { get; }
= new EmptyLookup<T, K>();
private EmptyLookup() { }
public bool Contains(T key) => false;
public int Count => 0;
public IEnumerable<K> this[T key] => Enumerable.Empty<K>();
public IEnumerator<IGrouping<T, K>> GetEnumerator()
=> Enumerable.Empty<IGrouping<K, V>>().GetEnumerator();
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
}
那么你可以寫這樣的代碼:
var x = EmptyLookup<int, int>.Instance;
創建新類的好處是您可以使用“is”運算符並檢查類型相等性:
if (x is EmptyLookup<,>) {
// ....
}
基於LukeH 的回答,我將創建一個帶有Empty<TKey, TElement>
方法的靜態類Lookup
。 通過這種方式,您可以使用與Enumerable.Empty<T>
相同的方式。
public static class Lookup
{
public static ILookup<TKey, TElement> Empty<TKey, TElement>()
=> Enumerable.Empty<TElement>().ToLookup(x => default(TKey));
}
示例用法: Lookup.Empty<string, string>()
創建一個空列表,然后對其執行 ToLookup(),如下所示:
List<Point> items = new List<Point>();
ILookup<int, int> lookup = items.ToLookup(p => p.X, p => p.Y);
祝你好運!
感謝@mqp的好主意。 我可以基於這種方法提出幾種擴展方法:
public static class IEnumerableExtensions
{
public static ILookup<TKey, TElement> ToEmptyLookup<TKey, TElement>(this IEnumerable<TElement> elements) => new TElement[0].ToLookup(k => default(TKey));
public static ILookup<TKey, TElement> ToEmptyLookup<TKey, TElement>(this IDictionary<TKey, TElement> elements) => new TElement[0].ToLookup(k => default(TKey));
public static ILookup<TKey, TElement> ToEmptyLookup<TKey, TElement>(this IGrouping<TKey, TElement> elements) => new TElement[0].ToLookup(k => default(TKey));
public static ILookup<TKey, TElement> ToEmptyLookup<TKey, TElement>(this IEnumerable<ILookup<TKey, TElement>> elements) => new TElement[0].ToLookup(k => default(TKey));
}
或者更符合 LINQ 精神的東西:
public static class Utility
{
public static ILookup<TKey, TElement> EmptyLookup<TKey, TElement>(Func<TKey, TKey> keySelector,
Func<TKey, TElement> elementSelector)
{
return Enumerable.Empty<TKey>().ToLookup(keySelector, elementSelector);
}
}
你可以返回空
或異常
但是你應該在課堂評論中注明
補充:+這比一些擴展方法更明顯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.