Compares two lists. Element order is important. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Compares two lists. Element order is important.

Demo Code

/********************************************************************
 *  FulcrumWeb RAD Framework - Fulcrum of your business             *
 *  Copyright (c) 2002-2010 FulcrumWeb, ALL RIGHTS RESERVED         *
 *                                                                  *
 *  THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      *
 *  FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        *
 *  COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       *
 *  AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  *
 *  AND PERMISSION FROM FULCRUMWEB. CONSULT THE END USER LICENSE    *
 *  AGREEMENT FOR INFORMATION ON ADDITIONAL RESTRICTIONS.           *
 ********************************************************************/
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Collections;
using System;/*from   ww w.  j  av a  2 s.  co m*/

public class Main{
        //-------------------------------------------------------------------------
    /// <summary>
    /// Compares two lists. Element order is important.
    /// </summary>
    /// <typeparam name="T">type of list elements</typeparam>
    /// <param name="l1">first list</param>
    /// <param name="l2">second list</param>
    /// <returns>true if lists are equal, otherwise false</returns>
    static public bool CompareOrdered<T>(IList l1, IList l2) where T: class
    {
      if (l1 == null && l2 == null)
      {
        return true;
      }
      if (l1 == null || l2 == null)
      {
        return false;
      }
      if (l1.Count != l2.Count)
      {
        return false;
      }
      for (int i = 0; i < l1.Count; i++)
      {
        if (!Equals(l1[i] as T, l2[i] as T))
        {
          return false;
        }
      }
      return true;
    }
        //-------------------------------------------------------------------------
    /// <summary>
    /// Compares two lists. Element order is important.
    /// </summary>
    /// <typeparam name="T">type of list elements</typeparam>
    /// <param name="l1">first list</param>
    /// <param name="l2">second list</param>
    /// <returns>true if lists are equal, false otherwise</returns>
    static public bool CompareOrdered<T>(IList<T> l1, IList<T> l2)
    {
      if (l1 == null && l2 == null)
      {
        return true;
      }
      if (l1 == null || l2 == null)
      {
        return false;
      }
      if (l1.Count != l2.Count)
      {
        return false;
      }
      for (int i = 0; i < l1.Count; i++)
      {
        if (!Equals(l1[i], l2[i]))
        {
          return false;
        }
      }
      return true;
    }
}

Related Tutorials