What should you do to implement IEquatable?

DRAG DROP
You have the following class:

You need to implement IEquatable. The Equals method must return true if both ID and Name are set
to the identical values. Otherwise, the method must return false. Equals must not throw an
exception.
What should you do? (Develop the solution by selecting and ordering the required code snippets.
You may not need all of the code snippets.)

DRAG DROP
You have the following class:

You need to implement IEquatable. The Equals method must return true if both ID and Name are set
to the identical values. Otherwise, the method must return false. Equals must not throw an
exception.
What should you do? (Develop the solution by selecting and ordering the required code snippets.
You may not need all of the code snippets.)

Answer: See the explanation

Explanation:
Box 1:

Box 2:

Box 3:



Leave a Reply 9

Your email address will not be published. Required fields are marked *


TheCoder

TheCoder

And you would need to return true at the end…

sv1slim

sv1slim

Anyone can explain why “if(!this.Name.Equals(other.Name)) return false” can’t be used?

guest

guest

if this.Name is null it will throw an exception

Ashraf

Ashraf

Object.Equals(….) internally checks if the received objects are null or in different types and return false.

Najlepszy Programista Swiata DAGO

Najlepszy Programista Swiata DAGO

5,7,1,4

Lord Vader

Lord Vader

any reason why we include if(other ==null) return false ? i assume it is to prevent an exception. but why are we not checking this.name if it is null?

Lord Vader

Lord Vader

are we only checking other==null to ensure that they are not equal as one is null

Lord Vader

Lord Vader

If this.Name is null, calling .Equals() on it will result in a NullReferenceException.