What is the purpose of 'volatile' keyword in C#

前端 未结 6 2233
抹茶落季
抹茶落季 2020-12-05 15:23

What is the purpose of volatile keyword in C#?

Where would I need to use this keyword?

I saw the following statement, but I am unable to underst

6条回答
  •  心在旅途
    2020-12-05 15:31

    It is nothing but telling to compiler that this variable will change its value at anytime by means of anything and compiler should not make any assumption about this variable.

    Normally compiler will assume that some variable will be constant during runtime. This may lead error in checking a registor value repeatedly. Because the register value may be changed by anything. So for these kind of variables should be declared 'volatile' and it be checked each time appears in the code with out any assumption.

提交回复
热议问题