Reputation: 5326
volatile XmlDocument d;
Just wondering why it is not a compile time error. I read that, we cannot make a field that is local as volatile. Please help me understand.
Upvotes: 0
Views: 185
Reputation: 81253
As per MSDN documentation of volatile keyword -
The volatile keyword can be applied to fields of these types:
Reference types.
Pointer types (in an unsafe context). Note that although the pointer itself can be volatile, the object that it points to cannot. In other words, you cannot declare a "pointer to volatile."
Types such as sbyte, byte, short, ushort, int, uint, char, float, and bool.
An enum type with one of the following base types: byte, sbyte, short, ushort, int, or uint.
Generic type parameters known to be reference types.
IntPtr and UIntPtr.
The volatile keyword can only be applied to fields of a class or struct. Local variables cannot be declared volatile.
As stated local variables in method cannot be marked volatile but its perfectly legal to have volatile class field.
Upvotes: 2