+ 2

# C# Operator-Overloading Lesson Exercise 2

Does not allow correct answer. Am I wrong?

6 ответов

+ 30

You have explanation in the COMMENTS :
➝ No, it's not 'Box'. Don't be confused by an example on the previous page. It was Box there, because the operator returned a Box object. Here it returns a boolean value, so it's 'bool'.
public static bool operator > (Box a, Box b)
{
if (a.Height*a.Width > b.Height*b.Width)
return true;
else
return false;
}

+ 23

David Carroll 😊Thank you!👍🍻

+ 6

Now, consider the next statements using comparison operators:
//Comparison Operators
var comparisonA = w1 > w2;
var comparisonB = A > B;
Both statements are comparing the first item with the second item. If the first is greater than the second, return true, otherwise, return false.
The return type is determined based on the statement using an arithmetic operator or comparison operator.

+ 5

Thank you both. My apologies i was not diligent enough in seeking the answer before i asked and for not being precise enough with my question. I will endeavor to do better next time.

+ 5

Bo Petrov No worries... Others seem to have been confused with this as well. 😉
I just posted this explanation in the comments for that Quiz question to help others with this...
For those confused about why overloading the > comparison operator returns bool instead of Box like the with + arithmetic operator, consider the following rationale:
What return type is expected with the statements below using arithmetic operators?
int w1 = 3;
int h1 = 6;
int w2 = 4;
int h2 = 5;
Box A = new Box(w1, h1);
Box B = new Box(w2, h2);
//Arithmetic Operators
var bigger_number = w1 + w2;
var bigger_box = A + B;
The first statement expects an integer and the second statement expects a Box. Adding two of the same type would result in the same type.
(continued...)

+ 4

Can you post the question?