StartsWith.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. /// Returns the source sequence prefixed with the specified value.
  11. /// </summary>
  12. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  13. /// <param name="source">Source sequence.</param>
  14. /// <param name="values">Values to prefix the sequence with.</param>
  15. /// <returns>Sequence starting with the specified prefix value, followed by the source sequence.</returns>
  16. public static IEnumerable<TSource> StartWith<TSource>(this IEnumerable<TSource> source, params TSource[] values)
  17. {
  18. if (source == null)
  19. throw new ArgumentNullException(nameof(source));
  20. return StartWithCore(source, values);
  21. }
  22. private static IEnumerable<TSource> StartWithCore<TSource>(IEnumerable<TSource> source, params TSource[] values)
  23. {
  24. foreach (var x in values)
  25. {
  26. yield return x;
  27. }
  28. foreach (var item in source)
  29. {
  30. yield return item;
  31. }
  32. }
  33. }
  34. }