List Equals - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:List

Description

List Equals

Demo Code

// Copyright (c) Microsoft. All rights reserved.
using System.Runtime.CompilerServices;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Diagnostics.Contracts;

public class Main{
        [Pure]//from w ww  .j a  va2  s  . c  om
        public static bool ListEquals<T>(this ICollection<T> first, ICollection<T> second)
        {
            if (first.Count != second.Count)
            {
                return false;
            }
            var cmp = EqualityComparer<T>.Default;
            var f = first.GetEnumerator();
            var s = second.GetEnumerator();
            while (f.MoveNext())
            {
                s.MoveNext();

                if (!cmp.Equals(f.Current, s.Current))
                {
                    return false;
                }
            }
            return true;
        }
}

Related Tutorials