How does 9.0 record realize value based equality for object properties (like Arrays)?

Viewed 1083

How does 9.0 record realize value based equality for object properties (like Arrays)?

I noticed that two record objects of same type having a timestamp property which is an array of bytes are not recognized as equal, despite all bytes where the same.

Example:

public record C 
{
   public byte[] Timestamp {get; set;}
}

https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQBMQGoA+BYAUAAQGYACBOAYwHsEViBhYgbz2NeKOOAE94BtAXWIAVAJZg4sCGAAOTAOZwYAbmJRFSgL54NQA===

will be realized as

public virtual bool Equals(C other)
{
    return (...) && EqualityComparer<byte[]>.Default.Equals(<Timestamp>k__BackingField, other.<Timestamp>k__BackingField);
}

it uses EqualityComparer which does not use SequenceEquals so the result is that two records with same content are not equal.

using System.Collections.Generic;
using System.Diagnostics;

var a = new byte[1] { 1 };
var b = new byte[1] { 1 };

var isEqual = EqualityComparer<byte[]>.Default.Equals(a, b);

Debug.Print(isEqual.ToString()); // false

I thought records would realize value based Equality for each property, but this is obviously still reference based for arrays in particular. Am I on the wrong track or is it just like that?

0 Answers
Related