You need 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 5

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


rao

rao

correct, but incomplete.
The last instruction would be:

return true;

Manab

Manab

if (other == null)return false;
if (this.ID != other.ID)return false;
if (!Object.Equals(this.Name,other.Name))return false;
return true;

Should be “if (!Object.Equals(this.Name,other.Name))return false;” because as MSDN says (https://msdn.microsoft.com/en-us/library/w4hkze5k(v=vs.110).aspx): firstly checked objects represent same object reference, secondly it determines whether either objA or objB is null, and finally it calls objA.Equals(objB) and returns the result

mariah

mariah

Why is ‘if (!this.Name.Equals(other.Name))’ not sufficient in this case ?

I don’t understand the expression ‘if (!Object.Equals(this.Name,other.Name))return false;’ here.
‘Object.Equals()’ offers referential equality, which is not necessary here
as you can see above : ” .. true if both ID and Name are set to the identical values .. “

TheCoder

TheCoder

what if this.Name is null, then you will get a exception…