开发者

operator++ in VB.NET - Interlocked.Increment

开发者 https://www.devze.com 2023-03-09 00:14 出处:网络
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(开发者_如何学Py

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(开发者_如何学PythonSystem.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?


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...


C# operator++ does not use interlocked semantics. It is equivalent to x=x+1.


No, it's not a correct replacement for current++ because current++ returns the unchanged value of current (post-increment), while

System.Math.Max(System.Threading.Interlocked.Increment(current),current - 1))

returns the incremented value (pre-increment).

I got this same code from a C# to VB converter http://www.developerfusion.com/tools/convert/csharp-to-vb/ and that difference changed the behavior of the program.

0

精彩评论

暂无评论...
验证码 换一张
取 消