What is the “volatile” keyword used for?

Technology CommunityCategory: C#What is the “volatile” keyword used for?
VietMX Staff asked 3 years ago

In C# volatile tells the compiler that the value of a variable must never be cached as its value may change outside of the scope of the program itself (such as the operating system, the hardware, or a concurrently executing thread.). The compiler will then avoid any optimisations that may result in problems if the variable changes “outside of its control”.