Enumerable.SequenceEqual Metoda

Definicja

Określa, czy dwie sekwencje są równe zgodnie z porównywaniem równości.

Przeciążenia

SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)

Określa, czy dwie sekwencje są równe, porównując elementy przy użyciu domyślnego porównania równości dla ich typu.

SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)

Określa, czy dwie sekwencje są równe, porównując ich elementy przy użyciu określonego IEqualityComparer<T>elementu .

SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)

Źródło:
SequenceEqual.cs
Źródło:
SequenceEqual.cs
Źródło:
SequenceEqual.cs

Określa, czy dwie sekwencje są równe, porównując elementy przy użyciu domyślnego porównania równości dla ich typu.

C#
public static bool SequenceEqual<TSource> (this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TSource> second);

Parametry typu

TSource

Typ elementów sekwencji wejściowych.

Parametry

first
IEnumerable<TSource>

Element IEnumerable<T> do porównania z second.

second
IEnumerable<TSource>

Element IEnumerable<T> do porównania z pierwszą sekwencją.

Zwraca

true jeśli dwie sekwencje źródłowe mają równą długość, a odpowiadające im elementy są równe zgodnie z domyślnym modułem porównania równości dla ich typu; w przeciwnym razie , false.

Wyjątki

first lub second ma wartość null.

Przykłady

W poniższych przykładach kodu pokazano, jak używać SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) metody w celu określenia, czy dwie sekwencje są równe. W pierwszych dwóch przykładach metoda określa, czy porównywane sekwencje zawierają odwołania do tych samych obiektów. W trzecim i czwartym przykładzie metoda porównuje rzeczywiste dane obiektów w sekwencjach.

W tym przykładzie sekwencje są równe.

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void SequenceEqualEx1()
{
    Pet pet1 = new Pet { Name = "Turbo", Age = 2 };
    Pet pet2 = new Pet { Name = "Peanut", Age = 8 };

    // Create two lists of pets.
    List<Pet> pets1 = new List<Pet> { pet1, pet2 };
    List<Pet> pets2 = new List<Pet> { pet1, pet2 };

    bool equal = pets1.SequenceEqual(pets2);

    Console.WriteLine(
        "The lists {0} equal.",
        equal ? "are" : "are not");
}

/*
 This code produces the following output:

 The lists are equal.
*/

Poniższy przykład kodu porównuje dwie sekwencje, które nie są równe. Należy pamiętać, że sekwencje zawierają identyczne dane, ale ponieważ obiekty, które zawierają, mają różne odwołania, sekwencje nie są traktowane jako równe.

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void SequenceEqualEx2()
{
    Pet pet1 = new Pet() { Name = "Turbo", Age = 2 };
    Pet pet2 = new Pet() { Name = "Peanut", Age = 8 };

    // Create two lists of pets.
    List<Pet> pets1 = new List<Pet> { pet1, pet2 };
    List<Pet> pets2 =
        new List<Pet> { new Pet { Name = "Turbo", Age = 2 },
                        new Pet { Name = "Peanut", Age = 8 } };

    bool equal = pets1.SequenceEqual(pets2);

    Console.WriteLine("The lists {0} equal.", equal ? "are" : "are not");
}

/*
 This code produces the following output:

 The lists are not equal.
*/

Jeśli chcesz porównać rzeczywiste dane obiektów w sekwencjach zamiast porównywać odwołania, musisz zaimplementować IEqualityComparer<T> interfejs ogólny w klasie. W poniższym przykładzie kodu pokazano, jak zaimplementować ten interfejs w klasie pomocniczej oraz udostępnić GetHashCode metody i Equals .

C#
public class ProductA : IEquatable<ProductA>
{
    public string Name { get; set; }
    public int Code { get; set; }

    public bool Equals(ProductA other)
    {
        if (other is null)
            return false;

        return this.Name == other.Name && this.Code == other.Code;
    }

    public override bool Equals(object obj) => Equals(obj as ProductA);
    public override int GetHashCode() => (Name, Code).GetHashCode();
}

Po zaimplementowaniu tego interfejsu można użyć sekwencji ProductA obiektów w metodzie SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) , jak pokazano w poniższym przykładzie:

C#

ProductA[] storeA = { new ProductA { Name = "apple", Code = 9 },
                       new ProductA { Name = "orange", Code = 4 } };

ProductA[] storeB = { new ProductA { Name = "apple", Code = 9 },
                       new ProductA { Name = "orange", Code = 4 } };

bool equalAB = storeA.SequenceEqual(storeB);

Console.WriteLine("Equal? " + equalAB);

/*
    This code produces the following output:

    Equal? True
*/

Uwagi

Metoda SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) wylicza dwie sekwencje źródłowe równolegle i porównuje odpowiednie elementy przy użyciu domyślnego porównania równości dla TSourceelementu , Default.

Domyślny moduł porównujący równości , Defaultsłuży do porównywania wartości typów. Aby porównać niestandardowy typ danych, należy zastąpić Equals metody i GetHashCode i i opcjonalnie zaimplementować IEquatable<T> interfejs ogólny w typie niestandardowym. Aby uzyskać więcej informacji, zobacz Default właściwość .

Dotyczy

.NET 9 i inne wersje
Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)

Źródło:
SequenceEqual.cs
Źródło:
SequenceEqual.cs
Źródło:
SequenceEqual.cs

Określa, czy dwie sekwencje są równe, porównując ich elementy przy użyciu określonego IEqualityComparer<T>elementu .

C#
public static bool SequenceEqual<TSource> (this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TSource> second, System.Collections.Generic.IEqualityComparer<TSource> comparer);
C#
public static bool SequenceEqual<TSource> (this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TSource> second, System.Collections.Generic.IEqualityComparer<TSource>? comparer);

Parametry typu

TSource

Typ elementów sekwencji wejściowych.

Parametry

first
IEnumerable<TSource>

Element IEnumerable<T> do porównania z second.

second
IEnumerable<TSource>

Element IEnumerable<T> do porównania z pierwszą sekwencją.

comparer
IEqualityComparer<TSource>

Element IEqualityComparer<T> do porównywania elementów.

Zwraca

truejeśli dwie sekwencje źródłowe mają równą długość, a ich odpowiadające im elementy są równe w zależności od comparer; w przeciwnym razie . false

Wyjątki

first lub second ma wartość null.

Przykłady

W poniższym przykładzie pokazano, jak zaimplementować porównywanie równości, którego można użyć w metodzie SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) .

C#
public class Product
{
    public string Name { get; set; }
    public int Code { get; set; }
}

// Custom comparer for the Product class
class ProductComparer : IEqualityComparer<Product>
{
    // Products are equal if their names and product numbers are equal.
    public bool Equals(Product x, Product y)
    {

        //Check whether the compared objects reference the same data.
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the products' properties are equal.
        return x.Code == y.Code && x.Name == y.Name;
    }

    // If Equals() returns true for a pair of objects
    // then GetHashCode() must return the same value for these objects.

    public int GetHashCode(Product product)
    {
        //Check whether the object is null
        if (Object.ReferenceEquals(product, null)) return 0;

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

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

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

Po zaimplementowaniu tego porównania można użyć sekwencji Product obiektów w metodzie SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) , jak pokazano w poniższym przykładzie:

C#

Product[] storeA = { new Product { Name = "apple", Code = 9 },
                       new Product { Name = "orange", Code = 4 } };

Product[] storeB = { new Product { Name = "apple", Code = 9 },
                       new Product { Name = "orange", Code = 4 } };

bool equalAB = storeA.SequenceEqual(storeB, new ProductComparer());

Console.WriteLine("Equal? " + equalAB);

/*
    This code produces the following output:

    Equal? True
*/

Uwagi

Metoda SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) wylicza dwie sekwencje źródłowe równolegle i porównuje odpowiednie elementy przy użyciu określonego IEqualityComparer<T>elementu . Jeśli comparer parametr ma nullwartość , domyślny moduł porównujący równości , Defaultsłuży do porównywania elementów.

Dotyczy

.NET 9 i inne wersje
Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0