Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator++ in VB.NET - Interlocked.Increment

Per the discussion here, one of the answers seems to imply that by using a code converter from C# to VB.NET, that the operator++ applied to an int should be replaced by System.Math.Max(System.Threading.Interlocked.Increment(current),current - 1)), I wondered if this is actually correct?

If so, why is it correct? I didn't think that operator++ would be implemented as an Interlocked.Increment operation? I didn't even think it was threadsafe. I'm failing to see who these two are the same, and then why the answer to the question linked to, even works?

I tried it and it produces the correct result. AFAIK, .NET has no such thing as undefined behaviour, as C++ does.

Can anybody clarify?

like image 364
Tony The Lion Avatar asked Feb 24 '23 01:02

Tony The Lion


1 Answers

You can do x += 1 in VB.Net. Not as elegant as x++, but better than x = x + 1.

Interlocked.Increment is for making your add operation thread-safe. You may not need it at all.

EDIT: Also, if it wasn't clear, there is no ++ operator in VB, I don't get why, but well...

like image 89
Neverbirth Avatar answered Feb 26 '23 16:02

Neverbirth