PrepAway - Latest Free Exam Questions & Answers

Which code segments should you include in Target 1, Target 2 and Target 3 to complete the code?

DRAG DROP
You are developing a class named Temperature.
You need to ensure that collections of Temperature objects are sortable.
You have the following code:

Which code segments should you include in Target 1, Target 2 and Target 3 to complete the code?
(To answer, drag the appropriate code segments to the correct targets in the answer are
a) Each code segment may be used once, more than once, or not at all. You may need to drag the
split bar between panes or scroll to view content.)

PrepAway - Latest Free Exam Questions & Answers

Answer: See the explanation

Explanation:
Target 1:

Target 2: CompareTo
Target 3:

Note:
Target 1:
The role of IComparable is to provide a method of comparing two objects of a particular type. This is
necessary if you want to provide any ordering capability for your object.
Incorrect: The role of IComparer is to provide additional comparison mechanisms. For example, you
may want to provide ordering of your class on several fields or properties, ascending and descending
order on the same field, or both.
Target 2, Target 3:
Example:
// Implement IComparable CompareTo method – provide default sort order.
int IComparable.CompareTo(object obj)
{

car c=(car)obj;
return String.Compare(this.make,c.make);
}

How to use the IComparable and IComparer interfaces in Visual C#
https://support.microsoft.com/en-us/kb/320727

3 Comments on “Which code segments should you include in Target 1, Target 2 and Target 3 to complete the code?


Leave a Reply