// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
using System.Collections;
using System.Collections.Generic;
namespace System.Linq
{
public static partial class EnumerableEx
{
///
/// Creates an enumerable sequence based on an enumerator factory function.
///
/// Result sequence element type.
/// Enumerator factory function.
/// Sequence that will invoke the enumerator factory upon a call to GetEnumerator.
public static IEnumerable Create(Func> getEnumerator)
{
if (getEnumerator == null)
throw new ArgumentNullException(nameof(getEnumerator));
return new AnonymousEnumerable(getEnumerator);
}
///
/// Creates an enumerable sequence based on an asynchronous method that provides a yielder.
///
/// Result sequence element type.
///
/// Delegate implementing an asynchronous method that can use the specified yielder to yield return
/// values.
///
/// Sequence that will use the asynchronous method to obtain its elements.
public static IEnumerable Create(Action> create)
{
if (create == null)
throw new ArgumentNullException(nameof(create));
foreach (var x in new Yielder(create))
{
yield return x;
}
}
private sealed class AnonymousEnumerable(Func> getEnumerator) : IEnumerable
{
public IEnumerator GetEnumerator() => getEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
}