Code inspection: Suspicious 'volatile' field usage: compound operation is not atomic. 'Interlocked' class can be used instead.
This inspection reports compound assignments and increment or decrement operations applied to a volatile field. Declaring a field as volatile does not make operations like +=, ++, --, or ??= atomic. These operations still perform multiple steps and can race when accessed from multiple threads.
Example
class Counter
{
private volatile int myValue;
void Increment()
{
myValue++;
}
}
using System.Threading;
class Counter
{
private int myValue;
void Increment()
{
Interlocked.Increment(ref myValue);
}
}
01 April 2026