Skip to content

Commit 07041cc

Browse files
authored
Add Index(...) LINQ method (#12)
1 parent bfa24b2 commit 07041cc

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

PolyShim.Tests/Net90/EnumerableExtensionsTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ namespace PolyShim.Tests.Net90;
77

88
public class EnumerableExtensionsTests
99
{
10+
[Fact]
11+
public void Index_Test()
12+
{
13+
// Arrange
14+
var source = new[] { 42, 13, 69, 17 };
15+
16+
// Act
17+
var result = source.Index();
18+
19+
// Assert
20+
result.Should().Equal((0, 42), (1, 13), (2, 69), (3, 17));
21+
}
22+
1023
[Fact]
1124
public void CountBy_Test()
1225
{

PolyShim/Net90/EnumerableExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ namespace System.Linq;
1111

1212
internal static partial class PolyfillExtensions
1313
{
14+
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.index
15+
public static IEnumerable<(int index, T value)> Index<T>(this IEnumerable<T> source) =>
16+
source.Select((value, index) => (index, value));
17+
1418
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.countby
1519
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(
1620
this IEnumerable<TSource> source,

0 commit comments

Comments
 (0)