SequenceEqual(TSource) 方法 (IEnumerable(TSource), IEnumerable(TSource), IEqualityComparer(TSource))
 本文章是由機器翻譯。 將指標移到文章內的文字上方即可查看原文。 其他資訊。 譯文 原文

Enumerable.SequenceEqual<TSource> 方法 (IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)

.NET Framework (current version)

語法

```public static bool SequenceEqual<TSource>(
this IEnumerable<TSource> first,
IEnumerable<TSource> second,
IEqualityComparer<TSource> comparer
)
```

first

second

comparer

傳回值

Type: System.Boolean

TSource

例外狀況

Exception Condition
ArgumentNullException

firstsecondnull

備註

SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) 方法會同時列舉兩個來源序列，並使用指定的 IEqualityComparer<T> 來比較對應的項目。如果 omparernull，則會使用預設相等比較子 Default 來比較項目。

範例

```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;
}

}
```

```
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
*/

```

版本資訊

Universal Windows Platform

.NET Framework

Portable Class Library

Silverlight

Windows Phone Silverlight

Windows Phone