|
# region Heading
/**************************************************************************************************************/
/* */
/* LibExt.ApplyIndices.cs */
/* */
/* Applies the provided Indices to the Things IList to yield an IEnumerable<T> of the indexed Things */
/* */
/* This is free code, use it as you require. If you modify it please use your own namespace. */
/* */
/* If you like it or have suggestions for improvements please let me know at: PIEBALDconsult@aol.com */
/* */
/* Modification history: */
/* 2009-07-20 Sir John E. Boucher Created */
/* */
/**************************************************************************************************************/
# endregion
namespace PIEBALD.Lib.LibExt.ApplyIndices
{
public static class LibExt
{
/**
<summary>
Applies the provided Indices to the Things to yield an IEnumerable<T>.
</summary>
<param name="Things">
An IList of items to which to apply Indices.
</param>
<param name="Indices">
A sequence of integers to use as indices.
</param>
<returns>
An IEnumerable<T> of the indexed Things.
</returns>
<exception cref="System.IndexOutOfRangeException">
If any of the provided Indices is out of range.
</exception>
*/
public static System.Collections.Generic.IEnumerable<T>
ApplyIndices<T>
(
this System.Collections.Generic.IList<T> Things
,
System.Collections.Generic.IEnumerable<int> Indices
)
{
int count = 0 ;
foreach
(
int index
in
Indices
)
{
T thing ;
try
{
thing = Things [ index ] ;
}
catch ( System.IndexOutOfRangeException err )
{
throw ( new System.IndexOutOfRangeException
(
System.String.Format
(
"Index {0} equals {1}, which is outside the range 0..{2}"
,
count
,
index
,
Things.Count - 1
)
,
err
) ) ;
}
yield return ( thing ) ;
count++ ;
}
yield break ;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.