Well, in many a cases (not int
, but say double
) we can have incomparable values where we can't say if they are equal or one of them is larger or smaller. The compiler aware of it (but it doesn't know the exact int
comparison implementation) so it complains:
what if num1
and num2
are incomparable? And all num1 > num2
, num1 < num2
, num1 == num2
return false
? What should be returned in such a case?
The easiest solution for you is to drop the last condition:
public string LargestNumber(int num1, int num2)
{
if (num1 > num2)
return("number 1 is the greatest!");
if (num1 < num2)
return("number 2 is the greatest!");
// We know, that there's one option here : to be equal
// Compiler doesn't know that all ints are comparable
return("Both are equal!");
}
please, note that in case of the same code but for double
the complainment makes sence.
Incomparable double
values exist:
public string LargestNumber(double num1, double num2)
{
if (num1 > num2)
return("number 1 is the greatest!");
if (num1 < num2)
return("number 2 is the greatest!");
if (num1 == num2)
return("Both are equal!");
return "Oops!";
}
Demo:
// double.NaN - Not a Number
// if floating point value is not a number, we can't just compare it!
// all >, <, == will return false!
Console.Write(LargestNumber(double.NaN, double.NaN));
Output:
Oops!