Enumerable.SequenceEqual(Of TSource) Method (IEnumerable(Of TSource), IEnumerable(Of TSource))

Determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.

Namespace:  System.Linq
Assemblies:   System.Linq (in System.Linq.dll)
  System.Core (in System.Core.dll)

'Declaration
<ExtensionAttribute> _
Public Shared Function SequenceEqual(Of TSource) ( _
	first As IEnumerable(Of TSource), _
	second As IEnumerable(Of TSource) _
) As Boolean

Type Parameters

TSource

The type of the elements of the input sequences.

Parameters

first
Type: System.Collections.Generic.IEnumerable(Of TSource)

An IEnumerable(Of T) to compare to second.

second
Type: System.Collections.Generic.IEnumerable(Of TSource)

An IEnumerable(Of T) to compare to the first sequence.

Return Value

Type: System.Boolean
true if the two source sequences are of equal length and their corresponding elements are equal according to the default equality comparer for their type; otherwise, false.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable(Of TSource). When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

ExceptionCondition
ArgumentNullException

first or second is Nothing.

The SequenceEqual(Of TSource)(IEnumerable(Of TSource), IEnumerable(Of TSource)) method enumerates the two source sequences in parallel and compares corresponding elements by using the default equality comparer for TSource, Default. The default equality comparer, Default, is used to compare values of the types that implement the IEqualityComparer(Of T) generic interface. To compare a custom data type, you need to implement this interface and provide your own GetHashCode and Equals methods for the type.

The following code examples demonstrate how to use SequenceEqual(Of TSource)(IEnumerable(Of TSource), IEnumerable(Of TSource)) to determine whether two sequences are equal. In the first two examples, the method determines whether the compared sequences contain references to the same objects. In the third and fourth examples, the method compares the actual data of the objects within the sequences.

In this example the sequences are equal.

    Class Pet
        Public Name As String 
        Public Age As Integer 
    End Class 

    Sub SequenceEqualEx1()
        ' Create two Pet objects. 
        Dim pet1 As New Pet With {.Name = "Turbo", .Age = 2}
        Dim pet2 As New Pet With {.Name = "Peanut", .Age = 8}

        ' Create two lists of pets. 
        Dim pets1 As New List(Of Pet)(New Pet() {pet1, pet2})
        Dim pets2 As New List(Of Pet)(New Pet() {pet1, pet2})

        'Determine if the two lists are equal. 
        Dim equal As Boolean = pets1.SequenceEqual(pets2)

        ' Display the output. 
        Dim text As String = IIf(equal, "are", "are not")
        MsgBox("The lists " & text & " equal.")

    End Sub 

    ' This code produces the following output: 
    
    ' The lists are equal.

The following code example compares two sequences that are not equal. Note that the sequences contain identical data, but because the objects that they contain have different references, the sequences are not considered equal.

        ' Create two Pet objects. 
        Dim pet1 As New Pet With {.Name = "Turbo", .Age = 2}
        Dim pet2 As New Pet With {.Name = "Peanut", .Age = 8}

        ' Create two lists of pets. 
        Dim pets1 As New List(Of Pet)()
        pets1.Add(pet1)
        pets1.Add(pet2)

        Dim pets2 As New List(Of Pet)()
        pets2.Add(New Pet With {.Name = "Turbo", .Age = 2})
        pets2.Add(New Pet With {.Name = "Peanut", .Age = 8})

        ' Determine if the two lists are equal. 
        Dim equal As Boolean = pets1.SequenceEqual(pets2)

        ' Display the output. 
        Dim text As String = IIf(equal, "are", "are not")
        MsgBox("The lists " & text & " equal.")

        ' This code produces the following output: 
        
        ' The lists are not equal.

If you want to compare the actual data of the objects in the sequences instead of just comparing their references, you have to implement the IEqualityComparer(Of T) generic interface in your class. The following code example shows how to implement this interface in a helper class and provide GetHashCode and Equals methods.

public class ProductA
{ 
    public string Name { get; set; }
    public int Code { get; set; }
}

public class ProductComparer : IEqualityComparer<ProductA>
{

    public bool Equals(ProductA x, ProductA y)
    {
        //Check whether the objects are the same object.  
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether the products' properties are equal.  
        return x != null && y != null && x.Code.Equals(y.Code) && x.Name.Equals(y.Name);
    }

    public int GetHashCode(ProductA obj)
    {
        //Get hash code for the Name field if it is not null.  
        int hashProductName = obj.Name == null ? 0 : obj.Name.GetHashCode();

        //Get hash code for the Code field.  
        int hashProductCode = obj.Code.GetHashCode();

        //Calculate the hash code for the product.  
        return hashProductName ^ hashProductCode;
    }
}

After you implement this interface, you can use sequences of ProductA objects in the SequenceEqual(Of TSource)(IEnumerable(Of TSource), IEnumerable(Of TSource)) method, as shown in the following example.


Dim storeA() As Product = 
    {New Product With {.Name = "apple", .Code = 9}, 
     New Product With {.Name = "orange", .Code = 4}}

Dim storeB() As Product = 
    {New Product With {.Name = "apple", .Code = 9}, 
     New Product With {.Name = "orange", .Code = 4}}

Dim equalAB = storeA.SequenceEqual(storeB)

Console.WriteLine("Equal? " & equalAB)

' This code produces the following output: 

' Equal? True

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft