What does compile time 'const' mean?

后端 未结 6 1442
执笔经年
执笔经年 2020-12-10 14:49

They say the difference between readonly and const is that const is compile-time (while readonly is run time). But what exactly does that mean, The fact that it\'s compile t

6条回答
  •  感动是毒
    2020-12-10 15:26

    One consequence of const being compile time is that changes to the const in one assembly does not get picked up automatically by other assemblies without recompiling them all.

    Eg:

    1. Assembly A has public const of int = 10
    2. Assembly B refers to that const.
    3. Both compiled. Now Assembly A const changed to 20, redeployed.
    4. Assembly B not recompiled.

    At runtime, Assembly B thinks the value of the const is still 10, not 20.

    If it were readonly, it would pick up the new value.

提交回复
热议问题