SequenceEqual(TSource) 方法 (IEnumerable(TSource), IEnumerable(TSource), IEqualityComparer(TSource))
 此文章由机器翻译。 将光标移到文章的句子上，以查看原文。 更多信息。 译文 原文

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

.NET Framework 4.6 and 4.5

## 语法

```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> 对相应的元素进行比较。如果 comparernull，则使用默认的相等比较器 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
4.5 后可用
.NET Framework
3.5 后可用
Portable Class Library

Silverlight
2.0 后可用
Windows Phone Silverlight
7.0 后可用
Windows Phone
8.1 后可用

## 请参阅

Microsoft 正在进行一项网上调查，以了解您对 MSDN 网站的意见。 如果您选择参加，我们将会在您离开 MSDN 网站时向您显示该网上调查。