C# Enumerable SequenceEqual(IEnumerable, IEnumerable)
Description
Determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.
Syntax
public static bool SequenceEqual<TSource>(
this IEnumerable<TSource> first,
IEnumerable<TSource> second
)
Parameters
TSource
- The type of the elements of the input sequences.first
- An IEnumerableto compare to second. second
- An IEnumerableto compare to the first sequence.
Returns
returns 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.
Example
The following code examples demonstrate how to use SequenceEqual to determine whether two sequences are equal.
//from w ww .j a va 2 s . c om
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
public static void Main(String[] argv){
Pet pet1 = new Pet { Name = "a", Age = 2 };
Pet pet2 = new Pet { Name = "b", Age = 8 };
List<Pet> pets1 = new List<Pet> { pet1, pet2 };
List<Pet> pets2 = new List<Pet> { pet1, pet2 };
bool equal = pets1.SequenceEqual(pets2);
Console.WriteLine(equal);
}
}
class Pet{
public string Name { get; set; }
public int Age { get; set; }
}
The code above generates the following result.