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 - C# Tutorial
From the course: C# Essential Training 1: Types and Control Flow
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Understanding classes, structs, and records7m
-
(Locked)
Defining constructors7m 6s
-
(Locked)
Object initialization6m 4s
-
(Locked)
Initialize only properties4m 59s
-
(Locked)
Cloning and copying objects9m 32s
-
(Locked)
Equality comparisons5m 27s
-
(Locked)
Defining abstract classes7m 52s
-
(Locked)
Creating static classes5m 18s
-
(Locked)
Solution: Expressing equality1m 5s
-
-
-
-
-
-