Checks if this enumeration has the given IEnumerable, applying the given to compare the equality of the elements. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Checks if this enumeration has the given IEnumerable, applying the given to compare the equality of the elements.

Demo Code

/*//from w  ww. j ava  2 s .c o  m
    Copyright (C) 2007-2017 Team MediaPortal
    http://www.team-mediaportal.com

    This file is part of MediaPortal 2

    MediaPortal 2 is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MediaPortal 2 is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MediaPortal 2. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;

public class Main{
        /// <summary>
    /// Checks if this enumeration has the given <paramref name="prefix"/>, applying the given <paramref name="comparer"/>
    /// to compare the equality of the elements.
    /// </summary>
    /// <typeparam name="T">Type of elements.</typeparam>
    /// <param name="check">This enumeration to check the given <paramref name="prefix"/>.</param>
    /// <param name="prefix">Prefix to check.</param>
    /// <param name="comparer">Equality comparer to determine equality of two elements.</param>
    /// <returns><c>true</c>, if this enumeration starts with the given <paramref name="prefix"/>, else <c>false</c>.</returns>
    public static bool StartsWith<T>(this IEnumerable<T> check, IEnumerable<T> prefix, IEqualityComparer<T> comparer)
    {
      IEnumerator<T> checkEnumer = check.GetEnumerator();
      return prefix.All(entry => checkEnumer.MoveNext() && comparer.Equals(checkEnumer.Current, entry));
    }
}

Related Tutorials