Microsoft Interview Question
Software Engineer in Tests1) A const cannot be static,while readonly can be static.
2) A const need to be declared and initialized at declaration only,while a readonly can be initialized at declaration or by the code in the constructor.
3) A const's value is evaluated at design time,while a readonly's value is evaluated at runtime.
Constant is a compile-time constant and will be inlined by the compile. This is why constants should be private or internal to avoid dependecy issues. It can contain only value types, except of string and null.
A readonly will be initialized either at its declaration or within the constructor and is not restricated to value types. Its value can only be changed via reflection.
In C# readonly allows initializing and modifying the value in constructor.
- guest June 28, 2009