Post-increment x by n (n != 1)

c#, post-increment

Solution

You should be able to abuse Interlocked.Exchange to get the old value of a variable while at the same time replacing its value:

Debug.WriteLine(Interlocked.Exchange(ref x, x+4));

in other words, replace the value of variable `x` with `x + 4` but return the previous value of `x`.

Edit:

Disassembly shows this "increment by 4 and swap" is done in 4 instructions - no call needed at all, so performance should be good:

            Interlocked.Exchange(ref x, x + 4);
0000005e  mov         eax,dword ptr [rbp+2Ch] 
00000061  add         eax,4 
00000064  xchg        eax,dword ptr [rbp+2Ch] 
00000067  mov         dword ptr [rbp+28h],eax 

The non-intuitive nature of this (and other) solutions probably boils down to the violation of the CQS principle - we are mutating a variable and returning a value all at once, i.e. not something we should be doing in the mainstream.

Problem

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.

Original source