Here is what MSDN says about volatile
:
The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times.
The volatile keyword can be applied to fields of these types: Reference types.
This state implies that fields of reference type are not volatile by default.
I think it's ok to treat reference-type field as a field of value type containing the address of the object. Then it becomes similar to int
type. Joe Albahari gives some examples.
But!... Unlike usual value types GC moves objects into memory when it compacts the heap and changes the references accordingly. Hence 'the most up-to-date value' must always be there. And if so how does the concept of volatility apply to reference types?