0
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.

Ahmad Ibrahim
  • 1,915
  • 2
  • 15
  • 32
Jasmine
  • 5,186
  • 16
  • 62
  • 114
  • 4
    Because a local is not a field, and a field is not a local... "local field" is an oxymoron – Marc Gravell Dec 01 '13 at 07:57
  • @MarcGravell: lol Marc, thanks, just realized. Still I have a question, I can just mark a private field within a class which is used by only one function within the same class as volatile. Why it doesn't provide any compile time intelligence to avoid unwanted usages like this? – Jasmine Dec 01 '13 at 08:00
  • and how should the compiler determine that it is unwanted? That's non-trivial – Marc Gravell Dec 01 '13 at 08:11
  • @MarcGravell: That's fine. So, is there any performance cost if a developer just use "volatile" keyword without a purpose? Or to ask you generically, there are many keywords we can use without a purpose too, but C# compiler doesn't even show warning, which is typically shows for things like "when we have a filed declared and never used it, it says "field declared but never used" "....Just out of curiosity I am asking this. What I try to understand is, is there any performance cost because of just using "volatile" keyword in unwanted places like this? – Jasmine Dec 01 '13 at 08:14
  • 2
    The cache (including via registers) is there for performance, so yes ignoring it and going to memory is slower. On a single field, probably not slow enough to ever notice. But if it was every single field: possibly – Marc Gravell Dec 01 '13 at 08:16
  • @MarcGravell: Thank you Marc, helps me understand a lot :) :) – Jasmine Dec 01 '13 at 08:18

1 Answers1

2

As per MSDN documentation of volatile keyword -

The volatile keyword can be applied to fields of these types:

  1. Reference types.

  2. 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."

  3. Types such as sbyte, byte, short, ushort, int, uint, char, float, and bool.

  4. An enum type with one of the following base types: byte, sbyte, short, ushort, int, or uint.

  5. Generic type parameters known to be reference types.

  6. 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.

Community
  • 1
  • 1
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Thanks Rohit, I misunderstood that local variable refers to the private fields within a class too. Still I have a question, I can just mark a private field within a class which is used by only one function within the same class as volatile. Why it doesn't provide any compile time intelligence to avoid unwanted usages like this? – Jasmine Dec 01 '13 at 07:59
  • You are mistaking volatile keyword. You can read/write private field from two different methods within same class. Consider scenario where one method executing on main thread and other method executing on background thread. – Rohit Vats Dec 01 '13 at 08:04
  • No, actually I understood volatile keyword. My question is, in the scenario mentioned above where I have ONLY ONE METHOD which uses it, why still the compiler let us use "volatile". It is unwanted to specify the field as volatile unless we have multithreaded or scenario where a field is likely to be changed by multiple functions. My simple question is, why compiler doesn't show a warning that I use volatile keyword without a purpose or even why it doesn't show compile time error – Jasmine Dec 01 '13 at 08:09
  • 1
    That question targets Microsoft people then but how you expect compiler to be sure that you are not using any thread other than main thread in your entire application process at compile time? – Rohit Vats Dec 01 '13 at 08:13
  • Uhmm no idea. I am just learning things, just I had a curiosity and asked :) Ok, let me read more and more on C# and .NET and get familiar :) – Jasmine Dec 01 '13 at 08:16
  • 1
    Using volatile do has some side effects so always try to avoid them. You can read more about it [here](http://stackoverflow.com/a/17530556/632337) in detail. – Rohit Vats Dec 01 '13 at 08:18