Table of Contents

Class EnumerableSizeComparer<T>

Namespace
Cuemon.Collections.Generic
Assembly
Cuemon.Core.dll

Provides IEnumerable<T> size comparison.

public class EnumerableSizeComparer<T> : Comparer<T>, IComparer<T>, IComparer where T : IEnumerable

Type Parameters

T

The IEnumerable<T> type to compare.

Inheritance
EnumerableSizeComparer<T>
Implements
Inherited Members

Examples

The following example demonstrates how to compare the sizes of enumerable collections using EnumerableSizeComparer. It shows comparisons between collections of different and equal sizes, including null-value handling.

using System;
using System.Collections;
using System.Collections.Generic;
using Cuemon.Collections.Generic;

namespace MyApp.Examples
{
    public class EnumerableSizeComparerExample
    {
        public static void Demonstrate()
        {
            var shortList = new[] { 10, 20, 30 };
            var longList = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var equalList = new[] { "x", "y", "z" };

            var comparer = EnumerableSizeComparer<IEnumerable>.Default;

            int result1 = comparer.Compare(shortList, longList);  // -1 (shortList has fewer elements)
            int result2 = comparer.Compare(longList, shortList);  //  1 (longList has more elements)
            int result3 = comparer.Compare(shortList, equalList); //  0 (both have 3 elements)
            int result4 = comparer.Compare(null, longList);       // -1 (null is less than any non-null)
            int result5 = comparer.Compare(shortList, null);      //  1 (non-null is greater than null)

            Console.WriteLine($"shortList vs longList : {result1}");
            Console.WriteLine($"longList vs shortList : {result2}");
            Console.WriteLine($"shortList vs equalList: {result3}");
            Console.WriteLine($"null vs longList      : {result4}");
            Console.WriteLine($"shortList vs null     : {result5}");

}}
}

Properties

Default

Returns a default comparer for the type specified by the generic argument.

public static IComparer<T> Default { get; }

Property Value

IComparer<T>

Methods

Compare(T, T)

Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.

public override int Compare(T x, T y)

Parameters

x T

The first object to compare.

y T

The second object to compare.

Returns

int

A signed integer that indicates the relative values of x and y, as explained here: Less than zero - x is less than y. Zero - x equals y. Greater than zero - x is greater than y.