Hide.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. /// Hides the enumerable sequence object identity.
  11. /// </summary>
  12. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  13. /// <param name="source">Source sequence.</param>
  14. /// <returns>Enumerable sequence with the same behavior as the original, but hiding the source object identity.</returns>
  15. /// <remarks>
  16. /// <see cref="Enumerable.AsEnumerable{TSource}(IEnumerable{TSource})"/> doesn't hide the object identity, and simply acts as a cast
  17. /// to the <see cref="IEnumerable{T}"/> interface type.
  18. /// </remarks>
  19. public static IEnumerable<TSource> Hide<TSource>(this IEnumerable<TSource> source)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. return HideCore(source);
  24. }
  25. private static IEnumerable<TSource> HideCore<TSource>(this IEnumerable<TSource> source)
  26. {
  27. foreach (var item in source)
  28. {
  29. yield return item;
  30. }
  31. }
  32. }
  33. }