IList Equal with IEqualityComparer - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

IList Equal with IEqualityComparer

Demo Code


using System.Linq;
using System.Collections.Generic;
using System;/*  ww  w  .  ja va  2s .  c om*/

public class Main{
        // TODO: Merge this file with CollectionExtensions.cs

        // This is not really an extension method, maybe it should go somewhere else.
        public static bool ArraysEqual<T>(IList<T> a1, IList<T> a2, IEqualityComparer<T> comparer = null)
        {
            if (ReferenceEquals(a1, a2))
                return true;

            if (a1 == null || a2 == null)
                return false;

            if (a1.Count != a2.Count)
                return false;

            if (comparer == null)
                comparer = EqualityComparer<T>.Default;
            for (int i = 0; i < a1.Count; i++)
            {
                if (!comparer.Equals(a1[i], a2[i]))
                    return false;
            }

            return true;
        }
}

Related Tutorials