-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:luoyunchong/FreeKit
- Loading branch information
Showing
3 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/IGeekFan.FreeKit.Extras/Extensions/EnumerableExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace IGeekFan.FreeKit.Extras.Extensions; | ||
|
||
/// <summary> | ||
/// IEnumerable 扩展 | ||
/// </summary> | ||
public static class EnumerableExtensions | ||
{ | ||
public static IEnumerable<(T item, int index)> LoopIndex<T>(this IEnumerable<T> self) => | ||
self.Select((item, index) => (item, index)); | ||
} |
31 changes: 31 additions & 0 deletions
31
test/IGeekFan.FreeKit.xUnit/Extras/Extensions/IEnumerableExtensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using IGeekFan.FreeKit.Extras.Extensions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace IGeekFan.FreeKit.xUnit.Extras.Extensions; | ||
|
||
public class EnumerableExtensionsTest | ||
{ | ||
private readonly ITestOutputHelper _testOutputHelper; | ||
|
||
public EnumerableExtensionsTest(ITestOutputHelper testOutputHelper) | ||
{ | ||
_testOutputHelper = testOutputHelper; | ||
} | ||
|
||
[Fact] | ||
public void LoopIndexTest() | ||
{ | ||
// Create a list with integer values | ||
List<int> myValues = new List<int>() | ||
{ | ||
23, 34, 45, 56, 67 | ||
}; | ||
|
||
// Loop through the list and its index using an item/index tuple | ||
foreach (var (item, index) in myValues.LoopIndex()) | ||
{ | ||
_testOutputHelper.WriteLine($"{index}: {item}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,5 @@ | |
</None> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |