Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StructuralComparisons for arrays

In F#:

[0] = [0] = true

In C# or .NET BCL in general:

StructuralComparisons.Equals(new int[] { 0 }, new int[] { 0 }) == false

Why?


Postscript:

The reason I thought I had the "right" Equals was because this turned out to be true:

var a = new { X = 3, Y = new { Z = -1 } };
var b = new { X = 3, Y = new { Z = -1 } };

StructuralComparisons.Equals(a, b) == true;
like image 424
Bent Rasmussen Avatar asked Aug 19 '15 12:08

Bent Rasmussen


1 Answers

That's because you're going down to object.Equals(objA, objB) which won't be able to handle that kind of comparison.

Instead do this:

StructuralComparisons.StructuralEqualityComparer.Equals(..., ...)
like image 158
Lasse V. Karlsen Avatar answered Nov 05 '22 10:11

Lasse V. Karlsen