In my specific use-case I have a couple of places that use Interlocked.Increment
and Interlocked.Decrement
interchangeably and so I wanted to consolidated the logic into a single method, and pass the correct method to be invoked, as argument:
if (Interlocked.Increment(ref x) == something)
{
DoThis();
}
else
{
DoThat();
}
And another place where I do:
if (Interlocked.Decrement(ref x) == something)
{
DoThis();
}
else
{
DoThat();
}
And the idea is to consolidate into:
void Foo(Action<ref int> interlockedOp)
{
if (interlockedOp(ref x) == something)
{
DoThis();
}
else
{
DoThat();
}
}
But the Action<ref int>
part doesn't compile.
Is there any other way to achieve this?