OnErrorResumeNext.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 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. {
  56. while (true)
  57. {
  58. var value = default(TSource);
  59. try
  60. {
  61. if (!innerEnumerator.MoveNext())
  62. break;
  63. value = innerEnumerator.Current;
  64. }
  65. catch
  66. {
  67. break;
  68. }
  69. yield return value;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }