Recently, we have collected some extension methods based on IEnumerable <T>. It brings us a lot of convenience and is sometimes practical. There are Alternate, Append, Prepend, Distinct, and Contains. For more information, see UnitTest.
Code:
/// <Summary>
/// IEnumerableExtension
/// </Summary>
Public static class IEnumerableExtensions
{
# Region Methods (5)
// Public Methods (5)
/// <Summary>
/// Alternates the specified first.
/// </Summary>
/// <Typeparam name = "TSource"> The type of the source. </typeparam>
/// <Param name = "first"> The first. </param>
/// <Param name = "second"> The second. </param>
/// <Returns> </returns>
Public static IEnumerable <TSource> Alternate <TSource> (this IEnumerable <TSource> first, IEnumerable <TSource> second)
{
Using (IEnumerator <TSource> e1 = first. GetEnumerator ())
Using (IEnumerator <TSource> e2 = second. GetEnumerator ())
While (e1.MoveNext () & e2.MoveNext ())
{
Yield return e1.Current;
Yield return e2.Current;
}
}
/// <Summary>
/// Appends the specified source.
/// </Summary>
/// <Typeparam name = "TSource"> The type of the source. </typeparam>
/// <Param name = "source"> The source. </param>
/// <Param name = "element"> The element. </param>
/// <Returns> IEnumerable <TSource> </returns>
Public static IEnumerable <TSource> Append <TSource> (this IEnumerable <TSource> source, TSource element)
{
Using (IEnumerator <TSource> e1 = source. GetEnumerator ())
While (e1.MoveNext ())
Yield return e1.Current;
Yield return element;
}
/// <Summary>
/// Determines whether [contains] [the specified source].
/// </Summary>
/// <Typeparam name = "TSource"> The type of the source. </typeparam>
/// <Typeparam name = "TResult"> The type of the result. </typeparam>
/// <Param name = "source"> The source. </param>
/// <Param name = "value"> The value. </param>
/// <Param name = "selector"> The selector. </param>
/// <Returns>
/// <C> true </c> if [contains] [the specified source]; otherwise, <c> false </c>.
/// </Returns>
Public static bool Contains <TSource, TResult> (
This IEnumerable <TSource> source, TResult value, Func <TSource, TResult> selector)
{
Foreach (TSource sourceItem in source)
{
TResult sourceValue = selector (sourceItem );
If (sourceValue. Equals (value ))
Return true;
}
Return false;
}
/// <Summary>
/// Distincts the specified source.
/// </Summary>
/// <Typeparam name = "TSource"> The type of the source. </typeparam>
/// <Typeparam name = "TResult"> The type of the result. </typeparam>
/// <Param name = "source"> The source. </param>
/// <Param name = "comparer"> The comparer. </param>
/// <Returns> IEnumerable <TSource> </returns>
Public static IEnumerable <TSource> Distinct <TSource, TResult> (
This IEnumerable <TSource> source, Func <TSource, TResult> comparer)
{
Return source. Distinct (new DynamicComparer <TSource, TResult> (comparer ));
}
/// <Summary>
/// Prepends the specified source.
/// </Summary>
& Nb