exam questions

Exam 70-483 All Questions

View all questions & answers for the 70-483 exam

Exam 70-483 topic 1 question 78 discussion

Actual exam question from Microsoft's 70-483
Question #: 78
Topic #: 1
[All 70-483 Questions]

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.)
Select and Place:

Show Suggested Answer Hide Answer
Suggested Answer:

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
patoyanez
Highly Voted 5 years, 10 months ago
the real answer is: if (other == null)return false; if (this.ID != other.ID)return false; if (!Object.Equals(this.Name,other.Name))return false; return true;
upvoted 50 times
BurgSharp
4 years, 5 months ago
return true; does the trick!
upvoted 4 times
...
...
Iryna
Highly Voted 5 years, 1 month ago
nope, the real answer is: if (other == null) return false; if (this.ID != other.ID) return false; if (!this.Name.Equals(other.Name)) return false; return true; Object.Equals will compare not strings, but the references
upvoted 7 times
tanujgyan
4 years, 8 months ago
.Equals will throw an an exception if Name is null. Go for object.Equals method
upvoted 6 times
...
...
banshee
Most Recent 5 years, 1 month ago
Well, for me it looks like both answers are correct, because "Equals" always compares values, not references, regardless if we call it on String or on Object. Am I correct?
upvoted 1 times
gaidys
5 years ago
But (!this.Name.Equals(other.Name)) return false; can throw exception if Name is null?
upvoted 3 times
natsk8freak
5 years ago
nope... According to Microsoft, it returns false. Name is declared as a String which has builtin handling for null values https://docs.microsoft.com/en-us/dotnet/api/system.string.equals?view=netframework-4.8
upvoted 2 times
ilCorvo
4 years, 10 months ago
I just performed a test and using Equals on a null string throws exception.
upvoted 10 times
...
VladWork
4 years, 8 months ago
If this.Name is null you will receive NRE
upvoted 5 times
...
...
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago