SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method EndsWith

    | Edit this page

    EndsWith<T>(IEnumerable<T>, IEnumerable<T>)

    Determines whether the end of the first sequence is equivalent to the second sequence, using the default equality comparer.

    Declaration
    public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second)
    Parameters
    Type Name Description
    IEnumerable<T> first

    The sequence to check.

    IEnumerable<T> second

    The sequence to compare to.

    Returns
    Type Description
    bool

    true if first ends with elements equivalent to second.

    Type Parameters
    Name Description
    T

    Type of elements.

    Remarks

    This is the IEnumerable<T> equivalent of EndsWith(string) and it calls Equals(T, T) using Default on pairs of elements at the same index.

    Examples

    The following code example demonstrates how to check that a sequence ends with another sequence using EndsWith.

    var sequence = Enumerable.Range(1, 5);
    
    // Determine if sequence ends with the ends sequence
    var ends = Enumerable.Range(4, 2);
    var result = sequence.EndsWith(ends);
    
    Console.WriteLine($"EndsWith: {result}");
    
    // This code produces the following output:
    // EndsWith: True
    
    Exceptions
    Type Condition
    ArgumentNullException

    first or second is null.

    | Edit this page

    EndsWith<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>?)

    Determines whether the end of the first sequence is equivalent to the second sequence, using the specified element equality comparer.

    Declaration
    public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second, IEqualityComparer<T>? comparer)
    Parameters
    Type Name Description
    IEnumerable<T> first

    The sequence to check.

    IEnumerable<T> second

    The sequence to compare to.

    IEqualityComparer<T> comparer

    Equality comparer to use.

    Returns
    Type Description
    bool

    true if first ends with elements equivalent to second.

    Type Parameters
    Name Description
    T

    Type of elements.

    Remarks

    This is the IEnumerable<T> equivalent of EndsWith(string) and it calls Equals(T, T) using Default on pairs of elements at the same index.

    Examples

    The following code example demonstrates how to check that a sequence ends with another sequence using EndsWith.

    var pets1 = new List<Pet> { new("Turbo", 2), new("Peanut", 8), };
    var pets2 = new List<Pet> { new("Peanut", 8), };
    
    // Determine if pets1 ends with the pets2 sequence.
    var result = pets1
        .EndsWith(
            pets2,
            new PetComparer());
    
    Console.WriteLine($"EndsWith: {result}");
    
    // This code produces the following output:
    // EndsWith: True
    
    class Pet
    {
        public Pet(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
    
        public string Name { get; }
        public int Age { get; }
    }
    
    class PetComparer : IEqualityComparer<Pet>
    {
        public bool Equals(Pet x, Pet y) =>
            x.Name == y.Name
            && x.Age == y.Age;
        public int GetHashCode(Pet x) =>
            HashCode.Combine(x.Name, x.Age);
    }
    
    Exceptions
    Type Condition
    ArgumentNullException

    first or second is null.

    © SuperLinq Authors. All rights reserved.