Search code examples
c#resharperconstantsreadonly

Why does ReSharper prefer consts over readonly?


I've noticed ReSharper suggestion under "Common Practices and Code Improvements": Convert local variable or field to constant.

I've also noticed that in Bill Wagner's book "Effective C#: 50 Specific Ways to Improve Your C#", there is a language idiom "Prefer readonly to const" in which the author explains the risks of using consts.

My question is not about the differences between readonly and const and when to use them, but why one source put const as a common practice/code improvement, and on the other hand, the second one treats readonly as an idiom?


Solution

  • Private constants do not carry the same risks as public constants. ReSharper is presumably suggesting performance optimizations for cases where a field is not externally visible.