From the course: C# Essential Training 1: Types and Control Flow

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Solution: Expressing equality

Solution: Expressing equality

- [Instructor] Hopefully you didn't spend too much time on this one, because what we want to do is change our type definition in order to allow it to be compared or check the equality by value. And we can do that by changing from a class to a record type. If I go down now and test my code, even though we've got some red squigglies, we should be fine. And we get a true. Now, one of the other things you may have tried is to change, not to a record, but a struct. And if you tried that, you probably saw something along these lines, saying the operator cannot be applied to operands of type, answer and answer. If you're doing those structs, then you can override that equality. But the simplest way is for us to use the record type that is compared by value. So when we create A and B, our variables down here, and they're two different answer instances, they're compared based on the values of their properties, name, age…

Contents