SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method CompareCount

    | Edit this page

    CompareCount<TFirst, TSecond>(IEnumerable<TFirst>, IEnumerable<TSecond>)

    Compares two sequences and returns an integer that indicates whether the first sequence has fewer, the same or more elements than the second sequence.

    Declaration
    public static int CompareCount<TFirst, TSecond>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second)
    Parameters
    Type Name Description
    IEnumerable<TFirst> first

    The first sequence.

    IEnumerable<TSecond> second

    The second sequence.

    Returns
    Type Description
    int

    -1 if the first sequence has the fewest elements, 0 if the two sequences have the same number of elements or 1 if the first sequence has the most elements.

    Type Parameters
    Name Description
    TFirst

    Element type of the first sequence.

    TSecond

    Element type of the second sequence.

    Remarks

    This method executes immediately.

    Examples

    The following code example demonstrates to compare the length of two sequences using CompareCount.

    var sequence = Enumerable.Range(1, 5);
                
    foreach (var x in Enumerable.Range(3, 5))
    {
        // Compare the length of two sequences
        var result = sequence.CompareCount(Enumerable.Range(1, x));
    
        Console.WriteLine($"CompareCount {x}: {result}");
    }
    
    // This code produces the following output:
    // CompareCount 3: 1
    // CompareCount 4: 1
    // CompareCount 5: 0
    // CompareCount 6: -1
    // CompareCount 7: -1
    
    Exceptions
    Type Condition
    ArgumentNullException

    first or second is null.

    © SuperLinq Authors. All rights reserved.