AllEqual - Kalkwst/Risotto GitHub Wiki
Checks if all elements in the sequence are equal.
In this page
- Definition
- Overloads
- AllEqual()
- AllEqual(IEqualityComparer comparer)
- AllEqual(Func<TSource, TValue> predicate)
- AllEqual(Func<TSource, TValue> predicate, IEqualityComparer comparer)
Overload | Description |
---|---|
AllEqual() | Iterates through all of the elements in the sequence and validates if they are all equal. |
AllEqual(IEqualityComparer comparer) | Iterates through all of the elements in the sequence and validates if they are all equal based on the provided comparer . |
AllEqual(Func<TSource, TValue> predicate) | Iterates through all of the elements in the sequence and validates if they are all equal, after applying the provided predicate on each element of the sequence. |
AllEqual(Func<TSource, TValue> predicate, IEqualityComparer predicate) | Iterates through all of the elements in the sequence and validates if they are all equal, after applying the provided predicate on each element of the sequence, based on the provided comparer . |
Determines whether all of the elements in the sequence are equal.
public static bool AllEqual<TSource>(this IEnumerable<TSource> source);
TSource
The type of the elements of source
.
source
IEnumerable <TSource>
A sequence of values to determine if they are all equal.
true
if all of the source sequence elements are equal; false
otherwise.
source
is null
The following code example demonstrates how to use AllEqual() to determine whether all of the elements in a sequence are equal.
int[] equalSource = new int[]{1, 1, 1, 1, 1, 1, 1, 1, 1};
equalSource.AllEqual()
//=> true
int[] notEqualSource = new int[]{1, 1, 1, 2, 1, 1, 1, 1};
//=> false
Enumeration is terminated as soon as a non-matching element is found.
Elements are compared to the first value of the sequence by using the default equality comparer EqualityComparer.Default
Determines whether all of the elements of the sequence are equal, by using a specified IEqualityComparer, for T
, the type of values in the sequence.
public static bool AllEqual<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer);
TSource
The type of the elements of source
.
source
IEnumerable <TSource>
A sequence of values to determine if they are all equal.
comparer
IEqualityComparer <TSource>
An equality comparer to compare values.
true
if all of the source sequence elements are equal; false
otherwise.
source
is null
- or -
comparer
is null
The following code example demonstrates how to implement an equality comparer that can be used in the AllEqual(IEqualityComparer comparer) method.
public class Resident
{
public string Name { get; set; }
public string ResidentId { get; set; }
public string ApartmentId { get; set; }
}
class ResidentComparer : IEqualityComparer<Resident>
{
// Residents are equal if their names and ResidentIds are equal.
public bool Equals(Resident x, Resident 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 residents' properties are equal.
return x.Name == y.Name && x.ResidentId == y.ResidentId;
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public int GetHashCode(Resident resident)
{
// Check whether the object is null
if (Object.ReferenceEquals(resident, null))
return 0;
// Get hash code for the Name field if it is not null.
int hashResidentName = resident.Name == null ? 0 : resident.Name.GetHashCode();
// Get hash code for the ResidentId field if it is not null.
int hashResidentId = resident.ResidentId == null ? 0 : resident.ResidentId.GetHashCode();
// Get hash code for the ApartmentId field
int hashApartmentId = resident.ResidentId.GetHashCode();
//Calculate the hash code for the resident.
return hashResidentName ^ hashResidentId ^ hashApartmentId;
}
}
After you implement this comparer, you can use a sequence of Resident
values in the AllEqual method as shown in the following example:
Resident[] residentsEqual = {
new Resident {
Name = "John Doe",
ResidentId = "547e95f0-e237-4b1b-8985-268d570abdc1",
ApartmentId = "16f82398-270d-4108-8c80-a877a7d4445b"
},
new Resident {
Name = "John Doe",
ResidentId = "547e95f0-e237-4b1b-8985-268d570abdc1",
ApartmentId = "16f82398-270d-4108-8c80-a877a7d4445b"
},
new Resident {
Name = "John Doe",
ResidentId = "547e95f0-e237-4b1b-8985-268d570abdc1",
ApartmentId = "3cede0e8-5229-484c-931d-be2d1901199a"
}
}
Resident[] residentsNotEqual = {
new Resident {
Name = "John Doe",
ResidentId = "547e95f0-e237-4b1b-8985-268d570abdc1",
ApartmentId = "16f82398-270d-4108-8c80-a877a7d4445b"
},
new Resident {
Name = "Michael Doe",
ResidentId = "547e95f0-e237-4b1b-8985-268d570abdc1",
ApartmentId = "16f82398-270d-4108-8c80-a877a7d4445b"
},
new Resident {
Name = "John Doe",
ResidentId = "547e95f0-e237-4b1b-8985-268d570abdc1",
ApartmentId = "3cede0e8-5229-484c-931d-be2d1901199a"
}
};
ResidentComparer resComp = new ResidentComparer();
bool areAllEqualFirst = residentsEqual.AllEqual(resComp);
bool areAllEqualSecond = residentsNotEqual.AllEqual(resComp);
Console.WriteLine("First? "+areAllEqualFirst);
Console.WriteLine("Second? "+areAllEqualSecond);
/*
This code produces the following output:
First? True
Second? False
*/
Enumeration is terminated as soon as a non-matching element is found.
Determines whether all of the elements of the sequence are equal, after applying a transformation on each element of the sequence, using the provided predicate
.
public static bool AllEqual<TSource, TValue>(IEnumerable<TSource> source, Func<TSource, TValue> predicate);
TSource
The type of the elements of source
.
TResult
The type of the value returned by the predicate
.
source
IEnumerable <TSource>
A sequence of values to determine if they are all equal.
predicate
Func <TSource, TResult>
A transform function to apply to each element.
true
if all of the source sequence elements are equal; false
otherwise.
source
is null
- or -
predicate
is null
The following code example demonstrates how to use AllEqual(Func<TSource, TValue> predicate) to determine whether all of the elements in a sequence are equal, after projecting a predicate
over the sequence.
int[] equalValues = new int[]{2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
equalValues.AllEqual(x => x * x);
//=> True
IEnumerable<int> nonEqualValues = Enumerable.Range(1, 10);
nonEqualValues .AllEqual(x => x * x);
//=> False
Enumeration is terminated as soon as a non-matching element is found.
Determines whether all of the elements of the sequence are equal by using a specified IEqualityComparer, after applying a transformation on each element of the sequence, using the provided predicate
.
public static bool AllEqual<TSource, TValue>(IEnumerable<TSource> source, Func<TSource, TValue> predicate, IEqualityComparer<TValue> comparer);
TSource
The type of the elements of source
.
TResult
The type of the value returned by the predicate
.
source
IEnumerable <TSource>
A sequence of values to determine if they are all equal.
predicate
Func <TSource, TResult>
A transform function to apply to each element.
comparer
IEqualityComparer <TSource>
An equality comparer to compare values.
true
if all of the source sequence elements are equal; false
otherwise.
source
is null
- or -
predicate
is null
- or -
comparer
is null
The following code example demonstrates how to use AllEqual(Func<TSource, TValue> predicate) to determine whether all of the elements in a sequence are equal, after projecting a predicate
over the sequence, with a custom IEqualityComparer.
int[] equalValues = new int[]{1, 2, 3, 4, 5};
equalValues.AllEqual(x => x * x, new ModuloComparer());
//=> True
int[] nonEqualValues = new int[]{1, 2, 3, 4, 5};
nonEqualValues.AllEqual(x => x * x * x, new ModuloComparer());
//=> False
Enumeration is terminated as soon as a non-matching element is found.