These days I'm more focused on polishing up my existing C# programming skills as well as attempting to learn more of the language with its new versions. I've begun to read a book titled "Effective C#: 50 Specific Ways to Improve Your C#" . I prefer to post the summary of each section of what I have learnt new from this book, which may be of useful to others too. Here is a summary of the usage of the const and readonly keywords:
- const is a Compile-Time constant where as the readonly is a Run-Time constant.
- Updating the value of a public constant should be viewed as an interface change.You must recompile all code that references that constant.
- Updating the value of a read-only constant is an implementation change; it is binary compatible with existing client code.
- To further elaborate the above points further:
- const must be used when the value must be available at compile time:
- Attribute parameters and enum definitions, and those rare times when you mean to define a value that does not change from release to release.
- For everything else, prefer the increased flexibility of readonly constants.