Constant fields are fields that cannot be modified. It needs to be assigned a value when declaring.
const int a = 5;
Read-only fields are initialized when declared, or you can set it in the constructor.
Let's see an example where read only fields are only initialized inside the constructor -
class Calculate { readonly int z; public Demo( ) { z = 20; } }
The above is the detailed content of What is the difference between the keywords const and readonly in C#?. For more information, please follow other related articles on the PHP Chinese website!