35

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

4

8 回答 8

58

除了mquanderVasile Bujac的答案之外,您可以创建一个漂亮、直接的单例EmptyLookup<K,E>类,如下所示。ILookup<K,E>(在我看来,按照 Vasile 的回答创建一个完整的实现似乎没有多大好处。)

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; }
    }
}
于 2011-07-25T20:20:28.177 回答
23

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));

I strongly doubt returning null would be more correct here. 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.

于 2011-07-25T19:53:21.590 回答
8

您可以为空查找创建一个单例类。

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<,>) {
 // ....
}
于 2011-07-25T19:55:17.337 回答
3

根据LukeH 的回答,我会创建一个Lookup带有Empty<TKey, TElement>方法的静态类。这样,您可以使用的方式与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>()

于 2019-03-11T09:24:40.923 回答
1

创建一个空列表,然后对其执行 ToLookup(),如下所示:

List<Point> items = new List<Point>();
ILookup<int, int> lookup = items.ToLookup(p => p.X, p => p.Y);

祝你好运!

于 2011-07-25T20:12:24.570 回答
0

感谢@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));
}
于 2020-07-17T08:43:06.143 回答
-1

或者更符合 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);
    }

}
于 2013-02-24T21:00:20.143 回答
-4

You can return null

or Exception

But you should note it in class comment

Added:+ This is more obvious than some extension method

于 2011-07-25T19:53:05.537 回答