// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
namespace System.Linq
{
public static partial class EnumerableEx
{
///
/// Creates a sequence that concatenates both given sequences, regardless of whether an error occurs.
///
/// Source sequence element type.
/// First sequence.
/// Second sequence.
/// Sequence concatenating the elements of both sequences, ignoring errors.
public static IEnumerable OnErrorResumeNext(this IEnumerable first, IEnumerable second)
{
if (first == null)
throw new ArgumentNullException(nameof(first));
if (second == null)
throw new ArgumentNullException(nameof(second));
return OnErrorResumeNextCore(new[] { first, second });
}
///
/// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the
/// sequences.
///
/// Source sequence element type.
/// Source sequences.
/// Sequence concatenating the elements of the given sequences, ignoring errors.
public static IEnumerable OnErrorResumeNext(params IEnumerable[] sources)
{
if (sources == null)
throw new ArgumentNullException(nameof(sources));
return OnErrorResumeNextCore(sources);
}
///
/// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the
/// sequences.
///
/// Source sequence element type.
/// Source sequences.
/// Sequence concatenating the elements of the given sequences, ignoring errors.
public static IEnumerable OnErrorResumeNext(this IEnumerable> sources)
{
if (sources == null)
throw new ArgumentNullException(nameof(sources));
return OnErrorResumeNextCore(sources);
}
private static IEnumerable OnErrorResumeNextCore(IEnumerable> sources)
{
foreach (var source in sources)
{
using var innerEnumerator = source.GetEnumerator();
while (true)
{
TSource value;
try
{
if (!innerEnumerator.MoveNext())
break;
value = innerEnumerator.Current;
}
catch
{
break;
}
yield return value;
}
}
}
}
}