OnErrorResumeNext.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class EnumerableEx
  11. {
  12. /// <summary>
  13. /// Creates a sequence that concatenates both given sequences, regardless of whether an error occurs.
  14. /// </summary>
  15. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  16. /// <param name="first">First sequence.</param>
  17. /// <param name="second">Second sequence.</param>
  18. /// <returns>Sequence concatenating the elements of both sequences, ignoring errors.</returns>
  19. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
  20. {
  21. if (first == null)
  22. throw new ArgumentNullException(nameof(first));
  23. if (second == null)
  24. throw new ArgumentNullException(nameof(second));
  25. return OnErrorResumeNext_(new[] {first, second});
  26. }
  27. /// <summary>
  28. /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the
  29. /// sequences.
  30. /// </summary>
  31. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  32. /// <param name="sources">Source sequences.</param>
  33. /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
  34. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(params IEnumerable<TSource>[] sources)
  35. {
  36. if (sources == null)
  37. throw new ArgumentNullException(nameof(sources));
  38. return OnErrorResumeNext_(sources);
  39. }
  40. /// <summary>
  41. /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the
  42. /// sequences.
  43. /// </summary>
  44. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  45. /// <param name="sources">Source sequences.</param>
  46. /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
  47. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
  48. {
  49. if (sources == null)
  50. throw new ArgumentNullException(nameof(sources));
  51. return OnErrorResumeNext_(sources);
  52. }
  53. private static IEnumerable<TSource> OnErrorResumeNext_<TSource>(IEnumerable<IEnumerable<TSource>> sources)
  54. {
  55. foreach (var source in sources)
  56. {
  57. using (var innerEnumerator = source.GetEnumerator())
  58. {
  59. while (true)
  60. {
  61. var value = default(TSource);
  62. try
  63. {
  64. if (!innerEnumerator.MoveNext())
  65. break;
  66. value = innerEnumerator.Current;
  67. }
  68. catch
  69. {
  70. break;
  71. }
  72. yield return value;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }