Skip to content

Commit

Permalink
Merge pull request #8 from jonsagara/feature/index
Browse files Browse the repository at this point in the history
Use the .NET 9 implementation, and make it .NET 8-only.
  • Loading branch information
jonsagara authored Mar 14, 2024
2 parents 4211e5b + 4fb411c commit b1732d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>12.0</LangVersion>

<!-- NuGet -->
<Version>2.0.14</Version>
<Version>2.0.15</Version>
<AssemblyVersion>2.0.0</AssemblyVersion>
<FileVersion>2.0.0</FileVersion>
<Authors>Jon Sagara</Authors>
Expand Down
23 changes: 21 additions & 2 deletions src/Sagara.Core/Extensions/IEnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public static class IEnumerableExtensions
{
#if NET6_0 || NET7_0 || NET8_0
#if NET8_0
/// <summary>
/// Returns an enumerable that incorporates the element's index into a tuple.
/// </summary>
Expand All @@ -15,7 +15,26 @@ public static class IEnumerableExtensions
{
ArgumentNullException.ThrowIfNull(source);

return source.Select((item, index) => (index, item));
if (source is TSource[] { Length: 0 })
{
return [];
}

return IndexIterator(source);
}

private static IEnumerable<(int Index, TSource Item)> IndexIterator<TSource>(IEnumerable<TSource> source)
{
int index = -1;
foreach (TSource element in source)
{
checked
{
index++;
}

yield return (index, element);
}
}
#endif
}

0 comments on commit b1732d3

Please sign in to comment.