I have 2 arrays :
int[] arr1 = new int[] { 1, 2, 3 };
int[] arr2 = new int[] { 1, 2, 3 };
I need to check if they are equal ( not by ref)
What is the difference between writing :
Console.WriteLine(arr1.SequenceEqual(arr2)); //true
vs
IStructuralEquatable eqArray1 = arr1;
Console.WriteLine(eqArray1.Equals(arr2, StructuralComparisons.StructuralEqualityComparer)); //true
both returns True..
When should I use each ?
The implementation of
SequenceEqualis kind of similar::This default
SequenceEqualmethod use defaultEqualityComparer<int>.Defaultforintwhich is value equality.ArrayimplementIStructuralEquatablewithEqualmethod:The
IEqualityComparerfrom input parameter is used, in here you inputStructruralEqualityComparerbutintdoes not implementIStructruralEquatable, so it uses default comparer forintwhich is value equality.But, needless to input
StructruralEqualityComparerbecauseintis not structural, you should just use:It still works. You should use
StructruralEqualityComparerif item in array is structruralSo to sum up, the implementation for both is kind of the same, both iterate two array based on value equality of
intto make comparison.I would prefer the LINQ verson since it is more readable.