繁体   English   中英

空 Ilookup<K, T>

[英]Empty ILookup<K, T>

我有一个返回ILookup的方法。 在某些情况下,我想返回一个空的ILookup作为提前退出。 构建空的ILookup的最佳方法是什么?

除了mquanderVasile 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM