From 6b940fd9733353c8db41005763b6c9063483040b Mon Sep 17 00:00:00 2001 From: Jon Sagara Date: Thu, 14 Mar 2024 10:52:51 -0700 Subject: [PATCH] #6: Add an Enumerable.Index extension method for .NET 6, 7, and 8. It's built into .NET 9. --- Directory.Build.props | 2 +- .../Extensions/IEnumerableExtensions.cs | 21 ++++++++++ .../docs/Sagara.Core.IEnumerableExtensions.md | 41 +++++++++++++++++++ src/Sagara.Core/docs/index.md | 2 + 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/Sagara.Core/Extensions/IEnumerableExtensions.cs create mode 100644 src/Sagara.Core/docs/Sagara.Core.IEnumerableExtensions.md diff --git a/Directory.Build.props b/Directory.Build.props index f01572a..3c59a5b 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -5,7 +5,7 @@ 12.0 - 2.0.13 + 2.0.14 2.0.0 2.0.0 Jon Sagara diff --git a/src/Sagara.Core/Extensions/IEnumerableExtensions.cs b/src/Sagara.Core/Extensions/IEnumerableExtensions.cs new file mode 100644 index 0000000..9e74fa8 --- /dev/null +++ b/src/Sagara.Core/Extensions/IEnumerableExtensions.cs @@ -0,0 +1,21 @@ +namespace Sagara.Core; + +public static class IEnumerableExtensions +{ +#if NET6_0 || NET7_0 || NET8_0 + /// + /// Returns an enumerable that incorporates the element's index into a tuple. + /// + /// + /// NOTE: This will be built into .NET 9 and above, so this method is for .NET 8 and below. + /// + /// The type of the elements of . + /// The source enumerable providing the elements. + public static IEnumerable<(int Index, TSource Item)> Index(this IEnumerable source) + { + ArgumentNullException.ThrowIfNull(source); + + return source.Select((item, index) => (index, item)); + } +#endif +} diff --git a/src/Sagara.Core/docs/Sagara.Core.IEnumerableExtensions.md b/src/Sagara.Core/docs/Sagara.Core.IEnumerableExtensions.md new file mode 100644 index 0000000..b782d25 --- /dev/null +++ b/src/Sagara.Core/docs/Sagara.Core.IEnumerableExtensions.md @@ -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 + + + +## IEnumerableExtensions.Index(this IEnumerable) 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(this System.Collections.Generic.IEnumerable source); +``` +#### Type parameters + + + +`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(this System.Collections.Generic.IEnumerable).source'). +#### Parameters + + + +`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(this System.Collections.Generic.IEnumerable).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(this System.Collections.Generic.IEnumerable).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. \ No newline at end of file diff --git a/src/Sagara.Core/docs/index.md b/src/Sagara.Core/docs/index.md index 864f254..a94dd71 100644 --- a/src/Sagara.Core/docs/index.md +++ b/src/Sagara.Core/docs/index.md @@ -24,6 +24,8 @@ - **[NotNull<T>(T, string, string, int, string)](Sagara.Core.Check.md#Sagara.Core.Check.NotNull_T_(T,string,string,int,string) 'Sagara.Core.Check.NotNull(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<T>(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, 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<TSource>(this IEnumerable<TSource>)](Sagara.Core.IEnumerableExtensions.md#Sagara.Core.IEnumerableExtensions.Index_TSource_(thisSystem.Collections.Generic.IEnumerable_TSource_) 'Sagara.Core.IEnumerableExtensions.Index(this System.Collections.Generic.IEnumerable)')** `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.