AllUnique - Kalkwst/Risotto GitHub Wiki
Checks if all elements in the sequence are unique.
In this page
- Definition
- Overloads
- AllUnique()
- AllUnique(IEqualityComparer comparer)
- AllUnique(Func<TSource, TValue> predicate)
- AllUnique(Func<TSource, TValue> predicate, IEqualityComparer comparer)
Overload | Description |
---|---|
AllUnique() | Iterates through all of the elements in the sequence and validates if they are all unique. |
AllUnique(IEqualityComparer comparer) | Iterates through all of the elements in the sequence and validates if they are all unique based on the provided comparer . |
AllUnique(Func<TSource, TValue> predicate) | Iterates through all of the elements in the sequence and validates if they are all unique, after applying the provided predicate on each element of the sequence. |
AllUnique(Func<TSource, TValue> predicate, IEqualityComparer predicate) | Iterates through all of the elements in the sequence and validates if they are all unique, 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 unique.
public static bool AllUnique<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 unique.
true
if all of the source sequence elements are unique; false
otherwise.
source
is null
The following code example demonstrates how to use AllUnique() to determine whether all of the elements in a sequence are unique.
int[] uniqueSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
uniqueSource.AllUnique();
//=> true
int[] notUniqueSource = new int[]{1, 1, 1, 2, 1, 1, 1, 1};
notUniqueSource.AllUnique();
//=> false
Determines whether all of the elements of the sequence are unique, by using a specified IEqualityComparer, for T
, the type of values in the sequence.
public static bool AllUnique<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 unique.
comparer
IEqualityComparer <TSource>
An equality comparer to compare values.
true
if all of the source sequence elements are unique; 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 AllUnique(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, ResidentIds and ApartmentIds 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 && x.AppartmentId == y.ApartmentId;
}
// 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 AllUnique method as shown in the following example:
Resident[] residentsUnique = {
new Resident {
Name = "John Doe",
ResidentId = "547e95f0-e237-4b1b-8985-268d570abdc1",
ApartmentId = "16f82398-270d-4108-8c80-a877a7d4445b"
},
new Resident {
Name = "Jane Doe",
ResidentId = "547e95f0-e237-4b1b-8985-268d570abdc1",
ApartmentId = "16f82398-270d-4108-8c80-a877a7d4445b"
},
new Resident {
Name = "Joss Doe",
ResidentId = "547e95f0-e237-4b1b-8985-268d570abdc1",
ApartmentId = "3cede0e8-5229-484c-931d-be2d1901199a"
}
}
Resident[] residentsNotUnique = {
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 = "16f82398-270d-4108-8c80-a877a7d4445b"
},
};
ResidentComparer resComp = new ResidentComparer();
bool areAllUniqueFirst = residentsUnique.AllUnique(resComp);
bool areAllUniqueSecond = residentsNotUnique.AllUnique(resComp);
Console.WriteLine("First? "+areAllUniequeFirst);
Console.WriteLine("Second? "+areAllUniqueSecond);
/*
This code produces the following output:
First? True
Second? False
*/
Determines whether all of the elements of the sequence are unique, after applying a transformation on each element of the sequence, using the provided predicate
.
public static bool AllUnique<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 unique.
predicate
Func <TSource, TResult>
A transform function to apply to each element.
true
if all of the source sequence elements are unique; false
otherwise.
source
is null
- or -
predicate
is null
The following code example demonstrates how to use AllUnique(Func<TSource, TValue> predicate) to determine whether all of the elements in a sequence are unique, after projecting a predicate
over the sequence.
int[] nonUniqueValues = new int[]{2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
nonUniqueValues.AllUnique(x => x * x);
//=> false
IEnumerable<int> uniqueValues = Enumerable.Range(1, 10);
uniqueValues .AllUnique(x => x * x);
//=> true
Determines whether all of the elements of the sequence are unique by using a specified IEqualityComparer, after applying a transformation on each element of the sequence, using the provided predicate
.
public static bool AllUnique<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 unique.
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 unique; false
otherwise.
source
is null
- or -
predicate
is null
- or -
comparer
is null
The following code example demonstrates how to use AllUnique(Func<TSource, TValue> predicate) to determine whether all of the elements in a sequence are unique, after projecting a predicate
over the sequence, with a custom IEqualityComparer.
int[] nonUniqueValues = new int[]{1, 2, 3, 4, 5};
equalValues.AllUnique(x => x * x, new ModuloComparer());
//=> False
int[] uniqueValues = new int[]{1, 2, 3, 4, 5};
nonEqualValues.AllUnique(x => x * x * x, new ModuloComparer());
//=> True