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

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

    This explains it. Summary: const must be initialized at declaration time, readonly can be initialized on the constructor (and thus have a different value depending on the constructor used).

    EDIT: See Gishu's gotcha above for the subtle difference

提交回复
热议问题