OnErrorResumeNext.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Linq
  6. {
  7. public static partial class EnumerableEx
  8. {
  9. /// <summary>
  10. /// Creates a sequence that concatenates both given sequences, regardless of whether an error occurs.
  11. /// </summary>
  12. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  13. /// <param name="first">First sequence.</param>
  14. /// <param name="second">Second sequence.</param>
  15. /// <returns>Sequence concatenating the elements of both sequences, ignoring errors.</returns>
  16. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
  17. {
  18. if (first == null)
  19. throw new ArgumentNullException(nameof(first));
  20. if (second == null)
  21. throw new ArgumentNullException(nameof(second));
  22. return OnErrorResumeNextCore(new[] { first, second });
  23. }
  24. /// <summary>
  25. /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the
  26. /// sequences.
  27. /// </summary>
  28. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  29. /// <param name="sources">Source sequences.</param>
  30. /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
  31. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(params IEnumerable<TSource>[] sources)
  32. {
  33. if (sources == null)
  34. throw new ArgumentNullException(nameof(sources));
  35. return OnErrorResumeNextCore(sources);
  36. }
  37. /// <summary>
  38. /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the
  39. /// sequences.
  40. /// </summary>
  41. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  42. /// <param name="sources">Source sequences.</param>
  43. /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
  44. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
  45. {
  46. if (sources == null)
  47. throw new ArgumentNullException(nameof(sources));
  48. return OnErrorResumeNextCore(sources);
  49. }
  50. private static IEnumerable<TSource> OnErrorResumeNextCore<TSource>(IEnumerable<IEnumerable<TSource>> sources)
  51. {
  52. foreach (var source in sources)
  53. {
  54. using var innerEnumerator = source.GetEnumerator();
  55. while (true)
  56. {
  57. TSource value;
  58. try
  59. {
  60. if (!innerEnumerator.MoveNext())
  61. break;
  62. value = innerEnumerator.Current;
  63. }
  64. catch
  65. {
  66. break;
  67. }
  68. yield return value;
  69. }
  70. }
  71. }
  72. }
  73. }