What is the “volatile” keyword used for?

前端 未结 8 978
一个人的身影
一个人的身影 2020-11-28 19:35

I read some articles about the volatile keyword but I could not figure out its correct usage. Could you please tell me what it should be used for in C# and in

8条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 20:24

    The volatile keyword has different meanings in both Java and C#.

    Java

    From the Java Language Spec :

    A field may be declared volatile, in which case the Java memory model ensures that all threads see a consistent value for the variable.

    C#

    From the C# Reference on the volatile keyword:

    The volatile keyword indicates that a field can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.

提交回复
热议问题