diff --git a/src/Polyfill/Polyfill_IEnumerable.cs b/src/Polyfill/Polyfill_IEnumerable.cs index 3b6dda2a..441a3d28 100644 --- a/src/Polyfill/Polyfill_IEnumerable.cs +++ b/src/Polyfill/Polyfill_IEnumerable.cs @@ -9,104 +9,4 @@ namespace Polyfills; static partial class Polyfill { - -#if !NET6_0_OR_GREATER - - /// - /// Attempts to determine the number of elements in a sequence without forcing an enumeration. - /// - /// The type of the elements of . - /// A sequence that contains elements to be counted. - /// - /// When this method returns, contains the count of if successful, - /// or zero if the method failed to determine the count. - /// - /// if the count of can be determined without enumeration; - /// otherwise, . - /// - /// - /// The method performs a series of type tests, identifying common subtypes whose - /// count can be determined without enumerating; this includes , - /// as well as internal types used in the LINQ implementation. - /// - /// The method is typically a constant-time operation, but ultimately this depends on the complexity - /// characteristics of the underlying collection implementation. - /// - [Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.trygetnonenumeratedcount")] - public static bool TryGetNonEnumeratedCount(this IEnumerable target, out int count) - { - if (target is ICollection genericCollection) - { - count = genericCollection.Count; - return true; - } - - if (target is ICollection collection) - { - count = collection.Count; - return true; - } - - count = 0; - return false; - } - -#endif - -#if NET46X || NET47 - - /// - /// Appends a value to the end of the sequence. - /// - /// A sequence of values. - /// The value to append to . - /// The type of the elements of source. - /// A new sequence that ends with element. - [Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.append")] - public static IEnumerable Append( - this IEnumerable target, - TSource element) - { - foreach (var item in target) - { - yield return item; - } - - yield return element; - } -#endif - -#if NETFRAMEWORK || NETSTANDARD2_0 - /// - /// Returns a new enumerable collection that contains the elements from source with the last count elements of the - /// source collection omitted. - /// - /// An enumerable collection instance. - /// The number of elements to omit from the end of the collection. - /// The type of the elements in the enumerable collection. - /// A new enumerable collection that contains the elements from source minus count elements from the end - /// of the collection. - [Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.skiplast")] - public static IEnumerable SkipLast( - this IEnumerable target, - int count) => - target.Reverse().Skip(count).Reverse(); -#endif - -#if NET471 || NET46X || NETSTANDARD2_0 - - /// - /// Creates a HashSet from an IEnumerable using the comparer to compare keys. - /// - /// An IEnumerable to create a HashSet from. - /// An IEqualityComparer to compare keys. - /// The type of the elements of source. - /// A HashSet that contains values of type TSource selected from the input sequence. - [Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.tohashset#system-linq-enumerable-tohashset-1(system-collections-generic-ienumerable((-0))-system-collections-generic-iequalitycomparer((-0)))")] - public static HashSet ToHashSet( - this IEnumerable target, - IEqualityComparer? comparer = null) => - new HashSet(target, comparer); - -#endif } \ No newline at end of file diff --git a/src/Polyfill/Polyfill_IEnumerable_Append.cs b/src/Polyfill/Polyfill_IEnumerable_Append.cs new file mode 100644 index 00000000..fae829b0 --- /dev/null +++ b/src/Polyfill/Polyfill_IEnumerable_Append.cs @@ -0,0 +1,33 @@ +// +#if NET46X || NET47 +#pragma warning disable + +namespace Polyfills; +using System.Collections; +using System.Collections.Generic; +using Link = System.ComponentModel.DescriptionAttribute; +using System.Linq; + +static partial class Polyfill +{ + /// + /// Appends a value to the end of the sequence. + /// + /// A sequence of values. + /// The value to append to . + /// The type of the elements of source. + /// A new sequence that ends with element. + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.append")] + public static IEnumerable Append( + this IEnumerable target, + TSource element) + { + foreach (var item in target) + { + yield return item; + } + + yield return element; + } +} +#endif \ No newline at end of file diff --git a/src/Polyfill/Polyfill_IEnumerable_SkipLast.cs b/src/Polyfill/Polyfill_IEnumerable_SkipLast.cs new file mode 100644 index 00000000..3193b18a --- /dev/null +++ b/src/Polyfill/Polyfill_IEnumerable_SkipLast.cs @@ -0,0 +1,28 @@ +// +#if NETFRAMEWORK || NETSTANDARD2_0 +#pragma warning disable + +namespace Polyfills; +using System.Collections; +using System.Collections.Generic; +using Link = System.ComponentModel.DescriptionAttribute; +using System.Linq; + +static partial class Polyfill +{ + /// + /// Returns a new enumerable collection that contains the elements from source with the last count elements of the + /// source collection omitted. + /// + /// An enumerable collection instance. + /// The number of elements to omit from the end of the collection. + /// The type of the elements in the enumerable collection. + /// A new enumerable collection that contains the elements from source minus count elements from the end + /// of the collection. + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.skiplast")] + public static IEnumerable SkipLast( + this IEnumerable target, + int count) => + target.Reverse().Skip(count).Reverse(); +} +#endif \ No newline at end of file diff --git a/src/Polyfill/Polyfill_IEnumerable_ToHashSet.cs b/src/Polyfill/Polyfill_IEnumerable_ToHashSet.cs new file mode 100644 index 00000000..8e924ad9 --- /dev/null +++ b/src/Polyfill/Polyfill_IEnumerable_ToHashSet.cs @@ -0,0 +1,28 @@ +// + +#if NET471 || NET46X || NETSTANDARD2_0 +#pragma warning disable + +namespace Polyfills; +using System.Collections; +using System.Collections.Generic; +using Link = System.ComponentModel.DescriptionAttribute; +using System.Linq; + +static partial class Polyfill +{ + /// + /// Creates a HashSet from an IEnumerable using the comparer to compare keys. + /// + /// An IEnumerable to create a HashSet from. + /// An IEqualityComparer to compare keys. + /// The type of the elements of source. + /// A HashSet that contains values of type TSource selected from the input sequence. + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.tohashset#system-linq-enumerable-tohashset-1(system-collections-generic-ienumerable((-0))-system-collections-generic-iequalitycomparer((-0)))")] + public static HashSet ToHashSet( + this IEnumerable target, + IEqualityComparer? comparer = null) => + new HashSet(target, comparer); + +} +#endif \ No newline at end of file diff --git a/src/Polyfill/Polyfill_IEnumerable_TryGetNonEnumeratedCount.cs b/src/Polyfill/Polyfill_IEnumerable_TryGetNonEnumeratedCount.cs new file mode 100644 index 00000000..4989fb70 --- /dev/null +++ b/src/Polyfill/Polyfill_IEnumerable_TryGetNonEnumeratedCount.cs @@ -0,0 +1,52 @@ +// +#if !NET6_0_OR_GREATER +#pragma warning disable + +namespace Polyfills; +using System.Collections; +using System.Collections.Generic; +using Link = System.ComponentModel.DescriptionAttribute; +using System.Linq; + +static partial class Polyfill +{ + /// + /// Attempts to determine the number of elements in a sequence without forcing an enumeration. + /// + /// The type of the elements of . + /// A sequence that contains elements to be counted. + /// + /// When this method returns, contains the count of if successful, + /// or zero if the method failed to determine the count. + /// + /// if the count of can be determined without enumeration; + /// otherwise, . + /// + /// + /// The method performs a series of type tests, identifying common subtypes whose + /// count can be determined without enumerating; this includes , + /// as well as internal types used in the LINQ implementation. + /// + /// The method is typically a constant-time operation, but ultimately this depends on the complexity + /// characteristics of the underlying collection implementation. + /// + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.trygetnonenumeratedcount")] + public static bool TryGetNonEnumeratedCount(this IEnumerable target, out int count) + { + if (target is ICollection genericCollection) + { + count = genericCollection.Count; + return true; + } + + if (target is ICollection collection) + { + count = collection.Count; + return true; + } + + count = 0; + return false; + } +} +#endif \ No newline at end of file