Extended method of IEnumerable

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.