Determines whether two generic Lists are equal. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:List

Description

Determines whether two generic Lists are equal.

Demo Code

/*//from  w w  w. ja v  a2  s.  c o  m
  FluorineFx open source library 
  Copyright (C) 2007 Zoltan Csibi, zoltan@TheSilentGroup.com, FluorineFx.com 
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Reflection;
using System.Collections;
using System;

public class Main{
        /// <summary>
        /// Determines whether two generic Lists are equal.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="a">A List object.</param>
        /// <param name="b">A List object.</param>
        /// <returns><b>true</b> if the lists are equal, otherwise <b>false</b>.</returns>
        /// <remarks>Two lists are considered equal if they have the same count and the default equality comparer for the generic argument determines that all elements are equal.</remarks>
        public static bool ListEquals<T>(IList<T> a, IList<T> b)
        {
            if (a == null || b == null)
                return (a == null && b == null);

            if (a.Count != b.Count)
                return false;

            var comparer = EqualityComparer<T>.Default;

            for (var i = 0; i < a.Count; i++)
            {
                if (!comparer.Equals(a[i], b[i]))
                    return false;
            }

            return true;
        }
}

Related Tutorials