Get differences from two Generic Lists

By mortl92 on Nov 15, 2012

If you have two lists of type List and you need to check if both lists contains the same entries ( case insensitive if you want) you can use this short snippet.

it returns true if the entries of both lists are not identical! you get the differences with the list called 'differences'!

szenario 1:
listA= {"A.iam","B.ipt","C.IPT"}
listB= {"a.iam","c.ipt","B.IPT"}

"Differences : False"

szenario 2:
listA= {"A.iam","B.ipt","C.IPT","E.txt","F.jpg"}
listB= {"a.iam","c.ipt","B.IPT", "D.iam"}

"Differences : True"
"Dif : E.txt"
"Dif : F.jpg"
"Dif : D.iam"

szenario 3:
listA= {"A.iam","B.ipt","C.IPT","b.IPT"}
listB= {"a.iam","c.ipt","B.IPT"}

"Differences : False"

Note:this snippet is helpful only if both of your lists contains unique entries (a.e. if listA would contain file-names from a directory A, and listB would contain file-names from directory B)!

void Main()
    List<string> listA = new List<string>() { "A.iam", "B.ipt", "C.IPT","E.txt","F.jpg" };
    List<string> listB = new List<string>() { "a.iam", "c.ipt", "B.IPT", "D.iam" };

    List<string> differences = new List<string>();

    System.Console.WriteLine("Differences : " + HasDifferences(listA, listB, out differences));

    foreach(var dif in differences)
        System.Console.WriteLine("Dif : "+dif);

public bool HasDifferences(List<string> listA, List<string> listB, out List<string> differences)
    differences = new List<string>();

    List<string> diff1 = listA.Except(listB, StringComparer.OrdinalIgnoreCase).ToList();
    List<string> diff2 = listB.Except(listA, StringComparer.OrdinalIgnoreCase).ToList();


        return false;
        return true;


Sign in to comment.
Hawkee   -  Nov 15, 2012

Good to see some .NET code.

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.