long vs {0L}[0]

天大地大妈咪最大 提交于 2019-12-22 14:16:09

问题


In one of our old services I found such piece of code (comments are original):

long[] tasksCounter = {0};  //boxing for long counters
long[] errorsCounter = {0}; //boxing for long counters

Further in the code these "arrays" are used with Interlocked class: Interlocked.Increment(ref errorsCounter[0]), Interlocked.Read(ref errorsCounter[0]) etc).

I wonder why did not the author use basicaly long tasksCounter, errorsCounter?

Probably this approach has benefits I don't know about?

It's probably worth mentioning that the variables are used in async lambda. When I change it to basic long Resharper plugin warns me with "Access to modified closure" and suggests to wrap it in array. I found couple of questions about it:

Access to Modified Closure

ReSharper Warning - Access to Modified Closure

来源:https://stackoverflow.com/questions/48193145/long-vs-0l0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!