What is the difference between const and readonly in C#?

前端 未结 30 2801
挽巷
挽巷 2020-11-22 05:05

What is the difference between const and readonly in C#?

When would you use one over the other?

30条回答
  •  伪装坚强ぢ
    2020-11-22 05:34

    • when to use const or readonly

      • const

        • compile-time constant: absolute constant, value is set during declaration, is in the IL code itself
      • readonly

        • run-time constant: can be set in the constructor/init via config file i.e. App.config, but once it initializes it can't be changed

提交回复
热议问题