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

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

    A const is a compile-time constant whereas readonly allows a value to be calculated at run-time and set in the constructor or field initializer. So, a 'const' is always constant but 'readonly' is read-only once it is assigned.

    Eric Lippert of the C# team has more information on different types of immutability

提交回复
热议问题