How does c# string evaluates its own value?
Asked Answered
S

5

7

Why does this snippet of code

string str = 30 + 20 + 10 + "ddd";
Console.WriteLine(str);

produces 60ddd,

and this one

string str = "ddd" + 30 + 20 + 10;
Console.WriteLine(str);

produces ddd302010?

Seems like it's very simple, but I can't get my head around it. Please, show me direction in which I can go in order to find a detailed answer.

Thanks!

Stinkweed answered 5/4, 2017 at 7:38 Comment(7)
Operator associativity and for C# specificallyBabysitter
Precedence and Order of EvaluationCounterpoint
@Babysitter Actually, in this case the ambiguity of operator +(int, int) and operator((string)int, string) is the cause of confusion.Counterpoint
However, at the end of the day, relying on Order of Evaluation is a prime example of how to ruin someone's day, and create untold damages from weird bugs.Counterpoint
@Counterpoint - no, it's the fact that because of the absence of parentheses, in the first expression, the first operation to be performed is clearly + between two ints and in the second expression, it's + between string and int. Due to associativity.Babysitter
@Babysitter WTF. Associativity is an overloaded term?! Why the hell! Associativity means something very different in mathematics, and given the mathematical formalism of functional programming languages, I assumed the mathematically term associativity.Counterpoint
@Babysitter Thanks for links!Stinkweed
H
11

The + operators in the expression you show have equal precedence because they're the same operator, hence are evaluated left to right:

30 + 20 + 10 + "ddd"
-- + (int, int) returns (int)50
------- + (int, int) returns (int)60
------------ + (object, string) returns (string)"60ddd"

Then for the other case:

"ddd" + 30 + 20 + 10
----- + (string, object) returns (string)"ddd30"
---------- + (string, object) returns (string)"ddd3020"
--------------- + (string, object) returns (string)"ddd302010"
Hofstetter answered 5/4, 2017 at 7:46 Comment(4)
Not all operators are evaluated left to right. Specifically, the first operator in the complete statements under consideration is the assignment operator and that's evaluated last. Similar 2 + 3 * 5 would not be evaluated left to right.Babysitter
@Damien absolutely correct, thanks for that. See edit, better?Hofstetter
I'd highlight that this is only because all of these operators are the same operator (and hence have equal precedence) and that + is left associative in C# (and most other languages as well)Babysitter
@Hofstetter Thanks, that makes perfect sense. Sometimes you just stuck on little things like this, after successfully conquering much more complicated topics)Stinkweed
B
2

It's because an expression is evaluated from left side to right side. In the first example 30 + 20 + 10 gives you int + string (30 + 20 + 10) - int, "ddd" - string. In the second example "ddd" + 30 is a string "ddd30" that appends "20" and "10" to it. It's all about the order (unless you have paranthesis).

Bet answered 5/4, 2017 at 7:41 Comment(0)
I
2

Operator + has different overloads:

int + int = int

int + string = string

string + int = string

In Following Expression:

string str = 30 + 20 + 10 + "ddd";
Console.WriteLine(str);

First 30 + 20 got evaluates both are integers so output of operator will be integer which is 50.

Then 50 + 10 will be evaluated which both are again integers so integer will be output which is 60.

Then 60 + "ddd" which is integer + string operation the operator in this case output string so 60 + "ddd" will output 60ddd.

In Following Expression:

string str = "ddd" + 30 + 20 + 10;
Console.WriteLine(str);

First "ddd" + 30 got evaluates in which string + integer operation takes place so output will be ddd30.

Then ddd30 + 20 will get evaluated in which again string + integer operation takes place so output will be ddd3020.

Then ddd3020 + 10 will get evaluated in which again string + integer operation takes place so output will be ddd302010.

Inserted answered 5/4, 2017 at 7:42 Comment(0)
V
2

It's evaluated from left to right. The first example has the numbers first, so it starts by evaluating as numbers. Then it finds out it has to evaluate as string. The second example is the other way around. It starts with string and continues with string.

Volant answered 5/4, 2017 at 7:42 Comment(0)
S
1

It happens because, the order of operations if from left to right. But assigment is last operation. To assing value first expression must be calculated.

Stanger answered 5/4, 2017 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.