-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#6: Add an Enumerable.Index extension method for .NET 6, 7, and 8. It…
…'s built into .NET 9.
- Loading branch information
Showing
4 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
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
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,21 @@ | ||
namespace Sagara.Core; | ||
|
||
public static class IEnumerableExtensions | ||
{ | ||
#if NET6_0 || NET7_0 || NET8_0 | ||
/// <summary> | ||
/// Returns an enumerable that incorporates the element's index into a tuple. | ||
/// </summary> | ||
/// <remarks> | ||
/// NOTE: This will be built into .NET 9 and above, so this method is for .NET 8 and below. | ||
/// </remarks> | ||
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> | ||
/// <param name="source">The source enumerable providing the elements.</param> | ||
public static IEnumerable<(int Index, TSource Item)> Index<TSource>(this IEnumerable<TSource> source) | ||
{ | ||
ArgumentNullException.ThrowIfNull(source); | ||
|
||
return source.Select((item, index) => (index, item)); | ||
} | ||
#endif | ||
} |
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,41 @@ | ||
#### [Sagara.Core](index.md 'index') | ||
### [Sagara.Core](index.md#Sagara.Core 'Sagara.Core') | ||
|
||
## IEnumerableExtensions Class | ||
|
||
```csharp | ||
public static class IEnumerableExtensions | ||
``` | ||
|
||
Inheritance [System.Object](https://docs.microsoft.com/en-us/dotnet/api/System.Object 'System.Object') 🡒 IEnumerableExtensions | ||
### Methods | ||
|
||
<a name='Sagara.Core.IEnumerableExtensions.Index_TSource_(thisSystem.Collections.Generic.IEnumerable_TSource_)'></a> | ||
|
||
## IEnumerableExtensions.Index<TSource>(this IEnumerable<TSource>) Method | ||
|
||
Returns an enumerable that incorporates the element's index into a tuple. | ||
|
||
```csharp | ||
public static System.Collections.Generic.IEnumerable<(int Index,TSource Item)> Index<TSource>(this System.Collections.Generic.IEnumerable<TSource> source); | ||
``` | ||
#### Type parameters | ||
|
||
<a name='Sagara.Core.IEnumerableExtensions.Index_TSource_(thisSystem.Collections.Generic.IEnumerable_TSource_).TSource'></a> | ||
|
||
`TSource` | ||
|
||
The type of the elements of [source](Sagara.Core.IEnumerableExtensions.md#Sagara.Core.IEnumerableExtensions.Index_TSource_(thisSystem.Collections.Generic.IEnumerable_TSource_).source 'Sagara.Core.IEnumerableExtensions.Index<TSource>(this System.Collections.Generic.IEnumerable<TSource>).source'). | ||
#### Parameters | ||
|
||
<a name='Sagara.Core.IEnumerableExtensions.Index_TSource_(thisSystem.Collections.Generic.IEnumerable_TSource_).source'></a> | ||
|
||
`source` [System.Collections.Generic.IEnumerable<](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1')[TSource](Sagara.Core.IEnumerableExtensions.md#Sagara.Core.IEnumerableExtensions.Index_TSource_(thisSystem.Collections.Generic.IEnumerable_TSource_).TSource 'Sagara.Core.IEnumerableExtensions.Index<TSource>(this System.Collections.Generic.IEnumerable<TSource>).TSource')[>](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1') | ||
The source enumerable providing the elements. | ||
|
||
#### Returns | ||
[System.Collections.Generic.IEnumerable<](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1')[<](https://docs.microsoft.com/en-us/dotnet/api/System.ValueTuple 'System.ValueTuple')[System.Int32](https://docs.microsoft.com/en-us/dotnet/api/System.Int32 'System.Int32')[,](https://docs.microsoft.com/en-us/dotnet/api/System.ValueTuple 'System.ValueTuple')[TSource](Sagara.Core.IEnumerableExtensions.md#Sagara.Core.IEnumerableExtensions.Index_TSource_(thisSystem.Collections.Generic.IEnumerable_TSource_).TSource 'Sagara.Core.IEnumerableExtensions.Index<TSource>(this System.Collections.Generic.IEnumerable<TSource>).TSource')[>](https://docs.microsoft.com/en-us/dotnet/api/System.ValueTuple 'System.ValueTuple')[>](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1') | ||
### Remarks | ||
NOTE: This will be built into .NET 9 and above, so this method is for .NET 8 and below. |
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