I have a record definition where I would like to only check equality on public/specific member variables in the record definition. I haven't found a way to do this without making a custom Equals function and I would prefer not to that if there is another nicer solution. Any reflections on whether private member variables in records is a bad pattern or not, is also appreciated.
Example of record where equality is true:
public record Test
{
public string Variable { get; init; }
public Test(string someValue)
{
Variable = someValue;
}
}
[Fact]
public void RecordTest()
{
var test1 = new Test("hello");
var test2 = new Test("hello");
Assert.Equal(test1, test2); // Passes
}
Example of record where I would like it to be true, but it's not:
public record Test
{
// I believe this is causing it to fail, can it be ignored somehow?
private readonly List<string> _list = new();
public string Variable { get; init; }
public Test(string someValue)
{
Variable = someValue;
_list.Add(someValue);
}
}
[Fact]
public void RecordTest()
{
var test1 = new Test("hello");
var test2 = new Test("hello");
Assert.Equal(test1, test2); // Fails
}