Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an Enumerable.Index extension method for .NET 6, 7, and 8 #7

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.13</Version>
<Version>2.0.14</Version>
<AssemblyVersion>2.0.0</AssemblyVersion>
<FileVersion>2.0.0</FileVersion>
<Authors>Jon Sagara</Authors>
Expand Down
21 changes: 21 additions & 0 deletions src/Sagara.Core/Extensions/IEnumerableExtensions.cs
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
}
41 changes: 41 additions & 0 deletions src/Sagara.Core/docs/Sagara.Core.IEnumerableExtensions.md
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') &#129106; 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&lt;](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')[&gt;](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&lt;](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1')[&lt;](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')[&gt;](https://docs.microsoft.com/en-us/dotnet/api/System.ValueTuple 'System.ValueTuple')[&gt;](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.
2 changes: 2 additions & 0 deletions src/Sagara.Core/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
- **[NotNull&lt;T&gt;(T, string, string, int, string)](Sagara.Core.Check.md#Sagara.Core.Check.NotNull_T_(T,string,string,int,string) 'Sagara.Core.Check.NotNull<T>(T, string, string, int, string)')** `Method` Throws an [System.ArgumentNullException](https://docs.microsoft.com/en-us/dotnet/api/System.ArgumentNullException 'System.ArgumentNullException') if the value is null.
- **[NotOutOfRange&lt;T&gt;(T, T, T, string, string, int, string)](Sagara.Core.Check.md#Sagara.Core.Check.NotOutOfRange_T_(T,T,T,string,string,int,string) 'Sagara.Core.Check.NotOutOfRange<T>(T, T, T, string, string, int, string)')** `Method` Throws an [System.ArgumentOutOfRangeException](https://docs.microsoft.com/en-us/dotnet/api/System.ArgumentOutOfRangeException 'System.ArgumentOutOfRangeException') if value is outside the range of
[rangeLo, rangeHi].
- **[IEnumerableExtensions](Sagara.Core.IEnumerableExtensions.md 'Sagara.Core.IEnumerableExtensions')** `Class`
- **[Index&lt;TSource&gt;(this IEnumerable&lt;TSource&gt;)](Sagara.Core.IEnumerableExtensions.md#Sagara.Core.IEnumerableExtensions.Index_TSource_(thisSystem.Collections.Generic.IEnumerable_TSource_) 'Sagara.Core.IEnumerableExtensions.Index<TSource>(this System.Collections.Generic.IEnumerable<TSource>)')** `Method` Returns an enumerable that incorporates the element's index into a tuple.
- **[SequentialGuid](Sagara.Core.SequentialGuid.md 'Sagara.Core.SequentialGuid')** `Class` Generates Guids sequentially.
- **[SequentialGuid()](Sagara.Core.SequentialGuid.md#Sagara.Core.SequentialGuid.SequentialGuid() 'Sagara.Core.SequentialGuid.SequentialGuid()')** `Constructor` Default .ctor. Initializes [CurrentGuid](Sagara.Core.SequentialGuid.md#Sagara.Core.SequentialGuid.CurrentGuid 'Sagara.Core.SequentialGuid.CurrentGuid') with a new value.
- **[SequentialGuid(Guid)](Sagara.Core.SequentialGuid.md#Sagara.Core.SequentialGuid.SequentialGuid(System.Guid) 'Sagara.Core.SequentialGuid.SequentialGuid(System.Guid)')** `Constructor` Constructor that initializes [CurrentGuid](Sagara.Core.SequentialGuid.md#Sagara.Core.SequentialGuid.CurrentGuid 'Sagara.Core.SequentialGuid.CurrentGuid') with the provided Guid.
Expand Down
Loading