C# standard question

R

Reginald Blue

This is purely out of curiosity.

In C and C++, the following statements invoke undefined behavior:

int i = 3;
i = i++;

(for reference:
http://www.eskimo.com/~scs/C-faq/q3.3.html )

Does the C# standard specify the behavior for such constructs? A quick test
reveals that the value assigned to i is 3 which is interesting, but not
particularly revealing.

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003 International
Conference on Intelligent User Interfaces]
 
J

Jon Skeet [C# MVP]

Reginald Blue said:
This is purely out of curiosity.

In C and C++, the following statements invoke undefined behavior:

int i = 3;
i = i++;

(for reference:
http://www.eskimo.com/~scs/C-faq/q3.3.html )

Does the C# standard specify the behavior for such constructs?
Yes.

A quick test reveals that the value assigned to i is 3 which is
interesting, but not particularly revealing.

It's fairly simple in C#. The RHS is evaluated and has a value of 3
while incrementing i to 4. The assignment is then performed, setting
the value of i back to 3.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top