5

I have the following line of code

int i = (i = 20);

and it sets the value of i to 20. Now my question is, are the two statements same ?

int a = 0;

int i = (a = 20);

and

int a = 0;

int i = a = 20;

Both the statements will set the values as i = 20 and a = 20. What's the difference?

If they are same then why are there braces for equating the value?

dlev
  • 48,024
  • 5
  • 125
  • 132
Harsha
  • 1,161
  • 4
  • 18
  • 38
  • They aren't braces, they're parentheses. – BoltClock Sep 12 '11 at 18:39
  • The braces (parenthesis) are around an assignment of the value. Not around an equation. And they're not needed. – H H Sep 12 '11 at 18:39
  • @BoltClock ya you're rite. I didn't know that parentheses will even work while assigning values. Interesting!!!. Thanks for all the answers. – Harsha Sep 12 '11 at 18:58

4 Answers4

11

From MSDN:

The assignment operators are right-associative, meaning that operations are grouped from right to left. For example, an expression of the form a = b = c is evaluated as a = (b = c).

sll
  • 61,540
  • 22
  • 104
  • 156
  • Though your quote is *correct* it is not *relevant*. It might appear that "int i = (i = 20);" is a parenthesization of two applications of the assignment operator, actually it is not; grammatically the first "=" is not an *operator* at all; it is part of the *variable declaration*. – Eric Lippert Sep 12 '11 at 20:28
  • @Eric : Thank you for the comment, for me it makes sense but why MSDN saying that tis is assignment operator? Also from original question the MSDN's example completely the same, just change i to a, b to a and c to 20 – sll Sep 12 '11 at 20:44
  • 3
    `a = b = c;` has two assignment operators. The operator is right-associative, so the side-effect of the rightmost operator happens first. `int a = b = c;` might look like the same thing, but it is not parsed as "int EXPRESSIONSTARTSHERE a = b = c EXPRESSIONENDSHERE ;" -- it is parsed as "int a = EXPRESSIONSTARTSHERE b = c EXPRESSIONENDSHERE ;" -- there is only *one* assignment operator. The other `=` is not an assignment *operator* at all; it is a local variable initializer. – Eric Lippert Sep 12 '11 at 20:56
  • @Eric : all is clear and makes sense, thanks! I will keep this in mind! – sll Sep 12 '11 at 21:00
7

Yes, those two are the same - but I would strongly discourage you from initializing variables like this. I would much rather see:

int a = 0;
// Whatever the intervening code is

a = 20;
int i = a;
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    Jon is right. Just because you "can" do something, doesn't mean you should. Readability / maintenance of code will always win out over trying to squeeze as much as you can into 1 line style thinking. – Ryan Ternier Sep 12 '11 at 18:44
3

are the two statements same ?

Yes. Just as

int x = 2 + 2;

and

int x = (2 + 2);

are the same.

What's the difference?

There is no difference. They're the same.

There are some rare cases in which there should be a difference between a parenthesized and an unparenthesized expression, but the C# compiler actually is lenient and allows them. See Is there a difference between return myVar vs. return (myVar)? for some examples.

If they are same then why are there braces for equating the value?

I don't understand the question. Can you clarify the question?

A question you did not ask:

Why is it legal to say "int i = (i = 20);"

It is a strange thing to do, but legal. The specification states that int i = x; is to be treated the same as int i; i = x; So therefore this should be the same as int i; i = (i = 20);.

Why then is that legal? Because (1) the result of an assignment operator is the value that was assigned. (See my article on the subject for details: http://blogs.msdn.com/b/ericlippert/archive/2010/02/11/chaining-simple-assignments-is-not-so-simple.aspx) And (2) because the definite assignment checker ensures that the local variable is not written to before it is read from. (It is never read from in this case; it is written to twice.)

Community
  • 1
  • 1
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
0

Order of Operations. When you put something in parenthesis it does that operation first.

And agreeing with the assignment of variables. It's better to do it vertically than horizontally. All on the same line can be confusing and more difficult with longer variables and assignments.

apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
  • 4
    Be careful. The *operator* runs first, but the *side effects of the computations of the operands* need not run first. That is, if you have A() + B() * C() then the * happens before the +, but the A() still happens before the B(). – Eric Lippert Sep 12 '11 at 18:49