PrepAway - Latest Free Exam Questions & Answers

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.)

PrepAway - Latest Free Exam Questions & Answers

Answer: See the explanation

Explanation:
Box 1:

Box 2:

Box 3:

6 Comments on “You need to implement IEquatable

  1. Manab says:

    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




    0



    0
    1. mariah says:

      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 .. “




      0



      0
      1. ivannathan says:

        >Why is ‘if (!this.Name.Equals(other.Name))’ not sufficient
        >in this case ?
        Because this.Name can be null and exception will be thrown. Question says that there shouldn’t be any exceptions.

        Object.equals will return false if this.Name is null




        0



        0

Leave a Reply