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

前端 未结 30 2854
挽巷
挽巷 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:30

    Constant variables are declared and initialized at compile time. The value can’t be changed after wards. Read-only variables will be initialized only from the Static constructor of the class. Read only is used only when we want to assign the value at run time.

提交回复
热议问题