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

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

.NET Framework 4.6 and 4.5

## 語法

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

first

second

#### 傳回值

Type: System.Boolean

TSource

## 例外狀況

Exception Condition
ArgumentNullException

firstsecondnull

## 備註

SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 方法會同時列舉兩個來源序列，並使用 TSource, Default 的預設相等比較子來比較對應的項目。預設相等比較子 Default 會用來比較實作 IEqualityComparer<T> 泛型介面的型別值。若要比較自訂資料型別，您必須實作這個介面，並為型別提供您自己的 GetHashCodeEquals 方法。

## 範例

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

```

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

```

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

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

```

## 版本資訊

Universal Windows Platform

.NET Framework

Portable Class Library

Silverlight

Windows Phone Silverlight

Windows Phone