I have a reference-type variable that is readonly
, because the reference never change, only its properties. When I tried to add the volatile
modifier to it the compiled warned me that it wouldn't let both modifiers apply to the same variable. But I think I need it to be volatile because I don't want to have caching problems when reading its properties. Am I missing anything? Or is the compiler wrong?
Update As Martin stated in one of the comments below: Both readonly and volatile modifiers apply only to the reference, and not to the object's properties, in the case of reference-type objects. That is what I was missing, so the compiler is right.
class C
{
readonly volatile string s; // error CS0678: 'C.s': a field cannot be both volatile and readonly
}