Skip to content

Commit aa4603c

Browse files
authored
Merge pull request #45 from I-RzR-I/feature/IIndexableEnumerable
Add a type of indexable enumerable
2 parents cb47874 + 9d9fcf7 commit aa4603c

8 files changed

Lines changed: 736 additions & 5 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022-2025 RzR
3+
Copyright (c) 2022-2026 RzR
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

docs/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### **v4.3.0.6204** [[RzR](mailto:108324929+I-RzR-I@users.noreply.github.com)] 11-01-2026
2+
* [09a89f2] (RzR) -> Add a type of indexed enumerable.
3+
14
### **v4.2.1.4986** [[RzR](mailto:108324929+I-RzR-I@users.noreply.github.com)] 04-12-2025
25
* [51d2aa9] (RzR) -> Auto commit uncommited files
36
* [9c27a62] (RzR) -> Upgrade deprecated package version.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// ***********************************************************************
2+
// Assembly : RzR.Shared.Extensions.DomainCommonExtensions
3+
// Author : RzR
4+
// Created On : 2026-01-06 19:01
5+
//
6+
// Last Modified By : RzR
7+
// Last Modified On : 2026-01-09 20:55
8+
// ***********************************************************************
9+
// <copyright file="IndexableEnumerableExtensions.cs" company="RzR SOFT & TECH">
10+
// Copyright © RzR. All rights reserved.
11+
// </copyright>
12+
//
13+
// <summary>
14+
// </summary>
15+
// ***********************************************************************
16+
17+
#region U S A G E S
18+
19+
using DomainCommonExtensions.Collections;
20+
using DomainCommonExtensions.Utilities.Ensure;
21+
using System;
22+
23+
#if NETSTANDARD1_5_OR_GREATER
24+
using System.Threading.Tasks;
25+
#endif
26+
27+
#endregion
28+
29+
namespace DomainCommonExtensions.ArraysExtensions
30+
{
31+
/// -------------------------------------------------------------------------------------------------
32+
/// <summary>
33+
/// An indexable enumerable extensions.
34+
/// </summary>
35+
/// =================================================================================================
36+
public static class IndexableEnumerableExtensions
37+
{
38+
/// -------------------------------------------------------------------------------------------------
39+
/// <summary>
40+
/// An IIndexableEnumerable&lt;T&gt; extension method that executes the action for each
41+
/// operation.
42+
/// </summary>
43+
/// <exception cref="ArgumentNullException">
44+
/// Thrown when one or more required arguments are null.
45+
/// </exception>
46+
/// <typeparam name="T">Generic type parameter.</typeparam>
47+
/// <param name="source">The source to act on.</param>
48+
/// <param name="action">The action.</param>
49+
/// =================================================================================================
50+
public static void DoActionForEach<T>(
51+
this IIndexableEnumerable<T> source,
52+
Action<T> action)
53+
{
54+
DomainEnsure.IsNotNull(source, nameof(source));
55+
DomainEnsure.IsNotNull(action, nameof(action));
56+
57+
for (int i = 0, count = source.Count; i < count; i++)
58+
action(source[i]);
59+
}
60+
61+
62+
#if NETSTANDARD1_5_OR_GREATER
63+
64+
/// -------------------------------------------------------------------------------------------------
65+
/// <summary>
66+
/// An IIndexableEnumerable&lt;T&gt; extension method that executes the action for each
67+
/// asynchronous operation.
68+
/// </summary>
69+
/// <exception cref="ArgumentNullException">
70+
/// Thrown when one or more required arguments are null.
71+
/// </exception>
72+
/// <typeparam name="T">Generic type parameter.</typeparam>
73+
/// <param name="source">The source to act on.</param>
74+
/// <param name="asyncAction">The asynchronous action.</param>
75+
/// <returns>
76+
/// A Task.
77+
/// </returns>
78+
/// =================================================================================================
79+
public static async Task DoActionForEachAsync<T>(
80+
this IIndexableEnumerable<T> source,
81+
Func<T, Task> asyncAction)
82+
{
83+
DomainEnsure.IsNotNull(source, nameof(source));
84+
DomainEnsure.IsNotNull(asyncAction, nameof(asyncAction));
85+
86+
for (int i = 0, count = source.Count; i < count; i++)
87+
await asyncAction(source[i]).ConfigureAwait(false);
88+
}
89+
90+
/// -------------------------------------------------------------------------------------------------
91+
/// <summary>
92+
/// An IIndexableEnumerable&lt;T&gt; extension method that executes the action for each
93+
/// asynchronous operation.
94+
/// </summary>
95+
/// <exception cref="ArgumentNullException">
96+
/// Thrown when one or more required arguments are null.
97+
/// </exception>
98+
/// <typeparam name="T">Generic type parameter.</typeparam>
99+
/// <param name="source">The source to act on.</param>
100+
/// <param name="asyncValueAction">The asynchronous action.</param>
101+
/// <returns>
102+
/// A Task.
103+
/// </returns>
104+
/// =================================================================================================
105+
public static async Task DoActionForEachAsync<T>(
106+
this IIndexableEnumerable<T> source,
107+
Func<T, ValueTask> asyncValueAction)
108+
{
109+
DomainEnsure.IsNotNull(source, nameof(source));
110+
DomainEnsure.IsNotNull(asyncValueAction, nameof(asyncValueAction));
111+
112+
for (int i = 0, count = source.Count; i < count; i++)
113+
await asyncValueAction(source[i]).ConfigureAwait(false);
114+
}
115+
116+
#endif
117+
}
118+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// ***********************************************************************
2+
// Assembly : RzR.Shared.Extensions.DomainCommonExtensions
3+
// Author : RzR
4+
// Created On : 2026-01-06 18:01
5+
//
6+
// Last Modified By : RzR
7+
// Last Modified On : 2026-01-06 20:11
8+
// ***********************************************************************
9+
// <copyright file="IndexableEnumerable.cs" company="RzR SOFT & TECH">
10+
// Copyright © RzR. All rights reserved.
11+
// </copyright>
12+
//
13+
// <summary>
14+
// </summary>
15+
// ***********************************************************************
16+
17+
#region U S A G E S
18+
19+
using System;
20+
using System.Collections;
21+
using System.Collections.Generic;
22+
using DomainCommonExtensions.Utilities.Ensure;
23+
24+
#endregion
25+
26+
namespace DomainCommonExtensions.Collections
27+
{
28+
/// -------------------------------------------------------------------------------------------------
29+
/// <summary>
30+
/// Indexable enumerable collection.
31+
/// </summary>
32+
/// <typeparam name="T">Generic type parameter.</typeparam>
33+
/// =================================================================================================
34+
public interface IIndexableEnumerable<out T> : IEnumerable<T>, IDisposable
35+
{
36+
/// -------------------------------------------------------------------------------------------------
37+
/// <summary>
38+
/// Indexer to get items within this collection using array index syntax.
39+
/// </summary>
40+
/// <param name="index">Zero-based index of the entry to access.</param>
41+
/// <returns>
42+
/// The indexed item.
43+
/// </returns>
44+
/// =================================================================================================
45+
T this[int index] { get; }
46+
47+
/// -------------------------------------------------------------------------------------------------
48+
/// <summary>
49+
/// Gets the number of records.
50+
/// </summary>
51+
/// <value>
52+
/// The count.
53+
/// </value>
54+
/// =================================================================================================
55+
int Count { get; }
56+
57+
/// -------------------------------------------------------------------------------------------------
58+
/// <summary>
59+
/// Executes for each operation.
60+
/// </summary>
61+
/// <param name="action">The action.</param>
62+
/// =================================================================================================
63+
void DoForEach(Action<T> action);
64+
}
65+
66+
/// -------------------------------------------------------------------------------------------------
67+
/// <inheritdoc cref="IIndexableEnumerable{T}"/>
68+
/// <typeparam name="T">Generic type parameter.</typeparam>
69+
/// <seealso cref="T:DomainCommonExtensions.Collections.IIndexableEnumerable{T}"/>
70+
/// =================================================================================================
71+
public sealed class IndexableEnumerable<T> : IIndexableEnumerable<T>
72+
{
73+
/// -------------------------------------------------------------------------------------------------
74+
/// <summary>
75+
/// (Immutable) the list.
76+
/// </summary>
77+
/// =================================================================================================
78+
private readonly IList<T> _list;
79+
80+
/// -------------------------------------------------------------------------------------------------
81+
/// <summary>
82+
/// Initializes a new instance of the <see cref="IndexableEnumerable{T}"/> class.
83+
/// </summary>
84+
/// <exception cref="ArgumentNullException">
85+
/// Thrown when one or more required arguments are null.
86+
/// </exception>
87+
/// <param name="list">(Immutable) the list.</param>
88+
/// =================================================================================================
89+
public IndexableEnumerable(IList<T> list)
90+
{
91+
DomainEnsure.IsNotNull(list, nameof(list));
92+
93+
_list = list;
94+
}
95+
96+
/// -------------------------------------------------------------------------------------------------
97+
/// <summary>
98+
/// Initializes a new instance of the <see cref="IndexableEnumerable{T}"/> class.
99+
/// </summary>
100+
/// <param name="array">The array.</param>
101+
/// =================================================================================================
102+
public IndexableEnumerable(T[] array)
103+
: this((IList<T>)array)
104+
{ }
105+
106+
/// -------------------------------------------------------------------------------------------------
107+
/// <summary>
108+
/// Initializes a new instance of the <see cref="IndexableEnumerable{T}"/> class.
109+
/// </summary>
110+
/// <param name="list">(Immutable) the list.</param>
111+
/// =================================================================================================
112+
public IndexableEnumerable(List<T> list)
113+
: this((IList<T>)list)
114+
{ }
115+
116+
/// -------------------------------------------------------------------------------------------------
117+
/// <summary>
118+
/// Initializes the index enumerable.
119+
/// </summary>
120+
/// <param name="list">(Immutable) the list.</param>
121+
/// <returns>
122+
/// An IIndexableEnumerable&lt;T&gt;
123+
/// </returns>
124+
/// =================================================================================================
125+
public static IIndexableEnumerable<T> Initialize(IList<T> list) => new IndexableEnumerable<T>(list);
126+
127+
/// -------------------------------------------------------------------------------------------------
128+
/// <summary>
129+
/// Initializes the index enumerable.
130+
/// </summary>
131+
/// <param name="array">The array.</param>
132+
/// <returns>
133+
/// An IIndexableEnumerable&lt;T&gt;
134+
/// </returns>
135+
/// =================================================================================================
136+
public static IIndexableEnumerable<T> Initialize(T[] array) => new IndexableEnumerable<T>(array);
137+
138+
/// -------------------------------------------------------------------------------------------------
139+
/// <summary>
140+
/// Initializes the index enumerable.
141+
/// </summary>
142+
/// <param name="list">(Immutable) the list.</param>
143+
/// <returns>
144+
/// An IIndexableEnumerable&lt;T&gt;
145+
/// </returns>
146+
/// =================================================================================================
147+
public static IIndexableEnumerable<T> Initialize(List<T> list) => new IndexableEnumerable<T>(list);
148+
149+
/// <inheritdoc />
150+
public T this[int index] => _list[index];
151+
152+
/// <inheritdoc />
153+
public int Count => _list.Count;
154+
155+
/// <inheritdoc />
156+
public void DoForEach(Action<T> action)
157+
{
158+
DomainEnsure.IsNotNull(action, nameof(action));
159+
160+
for (int i = 0, count = _list.Count; i < count; i++)
161+
action(_list[i]);
162+
}
163+
164+
/// <inheritdoc />
165+
public IEnumerator<T> GetEnumerator()
166+
=> _list.GetEnumerator();
167+
168+
IEnumerator IEnumerable.GetEnumerator()
169+
=> GetEnumerator();
170+
171+
/// <inheritdoc />
172+
public void Dispose() { _list.Clear(); }
173+
}
174+
}

0 commit comments

Comments
 (0)