// 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; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; 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 OnErrorResumeNext_(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 OnErrorResumeNext_(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 OnErrorResumeNext_(sources); } private static IEnumerable OnErrorResumeNext_(IEnumerable> sources) { foreach (var source in sources) { using (var innerEnumerator = source.GetEnumerator()) { while (true) { var value = default(TSource); try { if (!innerEnumerator.MoveNext()) break; value = innerEnumerator.Current; } catch { break; } yield return value; } } } } } }