One of the smartest ways to verify if two sequences of the type List(Of T) are equal, is to use LINQ. Or more specific, the extension method SequenceEqual(). It will compare both list in exact order and it returns a Boolean value, indicating if both lists are exactly the same.

Dim firstList = New List(Of Integer) From {10, 20, 30, 40, 50, 60, 70}
Dim secondList = New List(Of Integer) From {50, 40, 30, 20, 10}
 
' Use the SequenceEqual() extension method to compare two lists
Dim areEqual = firstList.SequenceEqual(secondList)
 
Console.WriteLine("Are equal: {0}", areEqual)

If you want to know which values are different, you can use the Except() extension method. It produces the set difference of two sequences by using the default equality comparer to compare values.

Dim firstList = New List(Of Integer) From {10, 20, 30, 40, 50, 60, 70}
Dim secondList = New List(Of Integer) From {50, 40, 30, 20, 10}
 
' Use the Except() extension method to get the differences of two lists
For Each difference In firstList.Except(secondList)
    Console.WriteLine(difference)
Next

As you can see in the screenshot below, the Except() method ignores the exact order of the values. The result of this piece of code is: 60 and 70, exact the two values which are not in the sequence secondList.

useexcepttogetdifferencesbetweentwolists

To play with these kind of code snippets I can really recommend LINQPad. It is not just for LINQ queries, but any C#/F#/VB expression, statement block or program! Instead of creating your next Visual Studio Console Application, use LINQPad. Starting from LINQPad version 5 Intellisense and auto-complete is supported for Visual Basic .NET too. Once you have used this really superb tool, you can’t imagine that you could ever live without it.