C# 9 introduces records, which among other benefits allow for really easy comparison. Is there some way I can take advantage of that functionality to compare a record composed of a collection of other records?
For example:
record Foo
{
string Name {get; set;}
List<Bar> Bars {get; set;}
public Foo(string name, params int[] values)
{
Name = name;
Bars = values.Select(v => new Bar(v)).ToList();
}
}
record Bar
{
int Value {get; set;}
public Bar(int value) => Value = value;
}
Somewhere else in the code:
var foo1 = new Foo("Hi",1,2,3);
var foo2 = new Foo("Hi",1,2,3);
return foo1 == foo2; // I want this to return true
By the way I am not looking for a solution to this specific piece of code. I know I can override the == operator or implement IComparable<Foo>, etc. My goal is to leverage built-in functionality so that I don't have to implement my own methods every time I want to compare a data container composed of a collection of data containers. Is there a way to do that?
Thanks!