-
In record classes we can override equality comparision by implementing duck typed public class Test
{
private class Class
{
public int Id { get; }
public Class(int id)
{
Id = id;
}
}
private record struct Record(Class Class)
{
public bool Equals(Record? other)
{
if (other is null) return false;
return other.Value.Class.Id == Class.Id;
}
}
[Fact]
public void MyTest()
{
var rec1 = new Record(new Class(0));
var rec2 = new Record(new Class(0));
Assert.Equal(rec1, rec2);
}
} How to override equality comparision for struct records? |
Beta Was this translation helpful? Give feedback.
Answered by
yaakov-h
Mar 2, 2023
Replies: 1 comment 3 replies
-
Take the |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
FLAMESpl
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Take the
?
out ofpublic bool Equals(Record? other)
. Since this is arecord struct
,Record?
isNullable<Record>
so the method does not have the correct signature to be the correctEquals(Record)
method.