2
Answers

Question about the parentheses

Tim Kangas

Tim Kangas

17y
2k
1

Look at this code snippet:

Console.WriteLine("9 > 7 is " + (9 > 7));

All it does is print "9 > 7 is True." to the screen. The parentheses are necessary otherwise the compiler complains saying "Operator ' >' cannot be applied to operands string and int.

I'm trying to understand the compilers message. All I understand is that by putting parentheses around the (9 > 7) part you force the compiler to evaluate what's in them first. So if I remove the parentheses, what is the compiler trying to do when it says "Operator '>' cannot be applied to operands string and int?

Is it trying to perform a comparison between the  string "9 > 7 is" and the > 7 or the 9 > 7 part?

Answers (2)