是否有可以使用的组合枚举器?

Is there combo enumerator ready to use?

我的 class 有两个集合,我正在使用 GetEnumerator 方法。我想为我的 class 提供 GetEnumerator 以及在这种意义上,首先在第一次收集之后进行枚举,然后进行第二次枚举。

是否准备好在 C# 中使用一些 ComboEnumerator class?

好奇心的小背景——第一个集合来自我对 Dictionary 的实现,第二个集合实际上只是一个元素,可选,包装在数组中。我想为其提供枚举器的 class 是从第一个 Dictionary 派生的 NullDictionary。所以我在寻找:

public override IEnumerator<KeyValuePair<K, V>> GetEnumerator()
{
    if (nullValue.HasValue)
        return ComboEnumerator.Create(
            new []{new KeyValuePair<K, V>(null, nullValue.Value)}.GetEnumerator(),
                base.GetEnumerator());
    else
        return base.GetEnumerator();
}

更新:我有自己的实现,这不是问题"how to implement XYZ"。

我认为您正在寻找的这种方法不存在。 如果您只想以标准方式枚举两个集合(returns 第一个集合的每个项目,然后第二个相同)我认为您可以使用 Enumerable.Concat 扩展方法。我看不出如何以更高效的方式做到这一点。
你可以看到 Enumerable.Concat method implementation

如果你想以非标准方式枚举集合(枚举这两个集合有多种方法)我认为你是唯一可以做到的,.net 框架无法为每个集合提供实现枚举两个集合的方法。