So, if I execute the following code...
int x = 0;
Debug.WriteLine(x++);
Debug.WriteLine(x += 4);
Debug.WriteLine(x);
... I get 0, 5, and 5, respectively. What I'd like to get, however is 0, 1, and 5. Is there any way to do a post-increment by n in C#? Or do I have to write out the += as its own statement?
Just for context, what I'm actually doing is a bunch of BitConverter operations on a buffer, and it'd be really nice to have each one as a self-sufficient statement where the offset is incremented by the size of the data type being converted to. That way, if the buffer format is later changed, I can just add or remove the one line without having to worry about any of the surrounding code.
zero
and aone
together out of those threeWriteLine
s. – Skellumx++
is syntactic sugar forx += 1
. This only works for+1
, all others have to be done using+= x
. – Cannelloni0
as output. – Cannelloni++x
is equivalent tox += 1
.x++
can't be written using+=
. – Sepalous