Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Need a little clarification (http://www.programmingforums.org/showthread.php?t=13712)

357mag Aug 6th, 2007 2:38 AM

Need a little clarification
 
Look at this line of code:

:

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

All it does is print "9 > 7 is True. The parentheses are needed because without them the compiler says this to me:

Operator '>' cannot be applied to operands of type string and int.

According to Herb, he says the parentheses are needed because the + operator has a higher precedence than the > operator.

I'm trying to understand the compilers message. I understand what Herb is saying. By putting parentheses around the (9 > 7) you force the compiler to evaluate those right away. I guess I don't quite understand what the compiler's message is though.

When I omit the parentheses, what does the compiler "think" I'm trying to do?

Booooze Aug 6th, 2007 2:55 AM

It thinks you are trying to write something to the console. Hence when you type Console.WriteLine. Your problem is that WriteLine only takes 1 parameter (as far as I know anyways). The first parameter anyways is a string. The error you're getting is telling you that you can't compare a string and an int using the '>' operator. You can put anything you want between the brackets for the parameter, but in the end, for it to work, it has to be a valid string. The reason it works with the code you provided is that, you had text and applied the + operator, meaning to add on to the string in this case, and you provided (9 > 7). It evaluates the equation in parentheses and connects it to the string.

To tell you the truth I'm a little surprised it works with the code you provided. I would have thought that when it evaluated (9 > 7), it would return true, but would throw you a similar error telling you you can't convert a bool to string.

357mag Aug 6th, 2007 3:34 AM

So without the parentheses is the compiler kind of seeing this whole thing as a string:

:

9 > 7 is + 9

I see the + 9 part as concatenating an integer onto the end of a string, but maybe the compiler sees it as concatenating a string(the character 9)onto the string "9 > 7".

lectricpharaoh Aug 6th, 2007 9:24 AM

Quote:

Originally Posted by 357mag
:

Console.WriteLine("9 > 7 is " + (9 > 7));
All it does is print "9 > 7 is True. The parentheses are needed because without them the compiler says this to me:

Operator '>' cannot be applied to operands of type string and int.

According to Herb, he says the parentheses are needed because the + operator has a higher precedence than the > operator.

Herb is not exactly wrong here- that is part of the problem- but he misses out the rest of the reason. In C++ and C#, you can do what is called 'operator overloading'. This just means you write a custom method that is invoked for the operator you replace. For example, in C++, the bit-shift operators, << and >>, are overloaded to provide output and input when applied to certain objects. Likewise, + is overloaded for the string class in C# to provide concatenation. However, the > operator is not. Because of this, it tries to do a regular comparison, which it cannot do (the types don't match), hence the error.
Quote:

Originally Posted by Booooze
To tell you the truth I'm a little surprised it works with the code you provided. I would have thought that when it evaluated (9 > 7), it would return true, but would throw you a similar error telling you you can't convert a bool to string.

The + operator is overloaded for all primitive types for the string class. Internally, it probably just 'boxes' the value, then calls the ToString() method of new boxed object. For object types, it just alls the ToString() method directly (one more good reason to have your own implementation of this method in your classes).

Booooze Aug 6th, 2007 2:33 PM

Aha, that explains it. Thanks.

357mag Aug 6th, 2007 3:49 PM

So without the parentheses around the (9 > 7) part, the compiler attempts to compare the string "9 > 7 is" with the > 7 or the 9 > 7 part? And because you can't compare a string to an int the compiler says "error".

Wizard1988 Aug 6th, 2007 4:17 PM

Order of operations;)

Dameon Aug 6th, 2007 9:30 PM

:

"9 > 7 is " + 9 > 7

is the same as

:

("9 > 7 is " + 9) > 7

if that makes it more clear.

357mag Aug 7th, 2007 4:36 PM

Yes that helps. I kind of suspected that.


All times are GMT -5. The time now is 3:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC