Generic lists - compare

Posted on 2009-12-19
Last Modified: 2012-05-08
I have 2 generic lists List<>. I would like to check if they are identical.
How do I do that?
Question by:johnkainn
    LVL 2

    Accepted Solution


    Please go through the following program...

    using System;  
    using System.Collections.Generic;
    using System.Linq;
    namespace ConsoleApplication1 {
         class ListCompareTest
             static void Main()
                 List<String> First = new List<string>() { "apple", "orange", "grape" };
                 List<String> Second = new List<string>() { "orange", "grape", "apple" };
                 bool trial = First.SequenceEqual<String>(Second);


    Hope this will help you to understand the concept.

    Shyju PG
    LVL 11

    Assisted Solution

    Above method definitely works but if you use more complex types than strings then you will have to implement your own comparer for it to work. Here is the msdn doc on how to do that:
    LVL 29

    Assisted Solution

    It seems abelallen's code has some errors. And definitely does not compile in VS2005.

    Try my sample code. It has some comments so I hope it'll be clear:
            private bool areEqual(List<String> first, List<String> second)
                bool result = false;
                // simple check to avoid extra work
                if (first.Count == second.Count)
                    // create a dictionary and add first list to it as keys
                    Dictionary<string, string> hash = new Dictionary<string, string>();
                    foreach (string ele in first)
                        hash.Add(ele, "");
                    // check each element from the second list is there
                    result = true;
                    foreach (string ele in second)
                            result = false;
                return result;
            private void button1_Click(object sender, EventArgs e)
                 List<String> first = new List<string>(new string[] { "apple", "orange", "grapes", "banana" });
                 List<String> second = new List<string> (new string[]{ "orange", "banana", "grape", "apple" });
                bool areListsEqual = areEqual(first, second);

    Open in new window

    LVL 2

    Expert Comment

    Hi anarki,

    Thanks for your comment. I knew it was a trivial solution. I thought of giving a small tablet for the light fever... :)

    Abel Allen

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video discusses moving either the default database or any database to a new volume.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now