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<T>
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<T>
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<T>
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