[英]Empty ILookup<K, T>
I have a method that returns an ILookup
.我有一个返回
ILookup
的方法。 In some cases I want to return an empty ILookup
as an early exit.在某些情况下,我想返回一个空的
ILookup
作为提前退出。 What is the best way of constructing an empty ILookup
?构建空的
ILookup
的最佳方法是什么?
Further to the answers from mquander and Vasile Bujac , you could create a nice, straightforward singleton-esque EmptyLookup<K,E>
class as follows.除了mquander和Vasile Bujac的答案之外,您还可以创建一个漂亮、直接的单例式
EmptyLookup<K,E>
类,如下所示。 (In my opinion, there doesn't seem much benefit to creating a full ILookup<K,E>
implementation as per Vasile's answer.) (在我看来,根据 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; }
}
}
There's no built-in, so I'd just write an extension method that runs something along the lines of new T[0].ToLookup<K, T>(x => default(K));
没有内置的,所以我只写了一个扩展方法,它沿着
new T[0].ToLookup<K, T>(x => default(K));
I strongly doubt returning null would be more correct here.我强烈怀疑在这里返回 null 会更正确。 It's almost never the case that you want to return null from a method which returns a collection (as opposed to an empty collection.) I could not possibly disagree more with people who are suggesting that.
您想从返回集合的方法(而不是空集合)中返回 null 的情况几乎从来没有。我不可能更不同意那些提出这一建议的人。
You can create a singleton class for empty lookups.您可以为空查找创建一个单例类。
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();
}
then you can write code like this:那么你可以写这样的代码:
var x = EmptyLookup<int, int>.Instance;
The benefit of creating a new class is that you can use the "is" operator and check for type equality:创建新类的好处是您可以使用“is”运算符并检查类型相等性:
if (x is EmptyLookup<,>) {
// ....
}
Based on LukeH answer, I'd create a static class Lookup
with an Empty<TKey, TElement>
method.基于LukeH 的回答,我将创建一个带有
Empty<TKey, TElement>
方法的静态类Lookup
。 In this way you could use is just the same way as Enumerable.Empty<T>
.通过这种方式,您可以使用与
Enumerable.Empty<T>
相同的方式。
public static class Lookup
{
public static ILookup<TKey, TElement> Empty<TKey, TElement>()
=> Enumerable.Empty<TElement>().ToLookup(x => default(TKey));
}
Example Usage: Lookup.Empty<string, string>()
示例用法:
Lookup.Empty<string, string>()
Create an empty list, then execute ToLookup() on it, like this:创建一个空列表,然后对其执行 ToLookup(),如下所示:
List<Point> items = new List<Point>();
ILookup<int, int> lookup = items.ToLookup(p => p.X, p => p.Y);
Good luck!祝你好运!
Thanks @mqp for the good idea.感谢@mqp的好主意。 I can propose several extension methods based on that approach:
我可以基于这种方法提出几种扩展方法:
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));
}
Or something more in the spirit of LINQ:或者更符合 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);
}
}
You can return null你可以返回空
or Exception或异常
But you should note it in class comment但是你应该在课堂评论中注明
Added:+ This is more obvious than some extension method补充:+这比一些扩展方法更明显
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.