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

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

    A constant will be compiled into the consumer as a literal value while the static string will serve as a reference to the value defined.

    As an exercise, try creating an external library and consume it in a console application, then alter the values in the library and recompile it (without recompiling the consumer program), drop the DLL into the directory and run the EXE manually, you should find that the constant string does not change.

提交回复
热议问题