Method CollectionEqual
| Edit this page View SourceCollectionEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Determines whether two collections are equal by comparing the elements by using the default equality comparer for their type.
Declaration
public static bool CollectionEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | first | An IEnumerable<T> to compare to |
IEnumerable<TSource> | second | An IEnumerable<T> to compare to the |
Returns
Type | Description |
---|---|
bool | 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. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of the input sequences. |
Remarks
This method uses the default equality comparer for TSource
, Default, determine whether two sequences have the same collection of elements.
A collection may have more than one of the same element, so this method will compare the value and count of
each element between both sequences.
This method executes immediately.
Examples
The following code example demonstrates how to compare two collections using the CollectionEqual
operator.
var pets1 = new List<Pet> { new("Turbo", 2), new("Peanut", 8), };
var pets2 = new List<Pet> { new("Peanut", 8), new("Turbo", 2), };
// Determine if the two collections are equal, using the default equality comparer
var result = pets1.CollectionEqual(pets2);
Console.WriteLine(result);
// This code produces the following output:
// True
record Pet(string Name, int Age);
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
CollectionEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>?)
Determines whether two collections are equal by comparing the elements by using a specified IEqualityComparer<T>.
Declaration
public static bool CollectionEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource>? comparer)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | first | An IEnumerable<T> to compare to |
IEnumerable<TSource> | second | An IEnumerable<T> to compare to the |
IEqualityComparer<TSource> | comparer | An IEqualityComparer<T> to use to compare elements. |
Returns
Type | Description |
---|---|
bool | 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. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of the input sequences. |
Remarks
This method uses the provided equality comparer for TSource
to determine whether two
sequences have the same collection of elements. A collection may have more than one of the same element, so
this method will compare the value and count of each element between both sequences. If comparer
is null, the default equality comparer, Default, is used.
This method executes immediately.
Examples
The following code example demonstrates how to compare two collections using the CollectionEqual
operator.
var pets1 = new List<Pet> { new("Turbo", 2), new("Peanut", 8), };
var pets2 = new List<Pet> { new("Peanut", 8), new("Turbo", 2), };
// Determine if the two collections are equal, using a custom equality comparer
var result = pets1
.CollectionEqual(
pets2,
new PetComparer());
Console.WriteLine(result);
// This code produces the following output:
// True
class Pet
{
public Pet(string name, int age)
{
this.Name = name;
this.Age = age;
}
public string Name { get; }
public int Age { get; }
}
class PetComparer : IEqualityComparer<Pet>
{
public bool Equals(Pet x, Pet y) =>
x.Name == y.Name
&& x.Age == y.Age;
public int GetHashCode(Pet x) =>
HashCode.Combine(x.Name, x.Age);
}
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|