What is a difference between the const and readonly keywords?
Answers
Answered by
0
Constant
1. Constant are static by default
2. They must have a value at compilation-time
(you can have e.g. 3.14*2+5, but cannot call methods).
3. Could be declared within functions
4. These are copied into every assembly that uses them (every assembly gets a local copy of values).
Readonly
1. Must have set value, by the time constructor exits.
2. Are evaluated when instance is created.
3. You can use static modifier for readonly fields.
4. readonly modifier can be used with reference types.
5. readonly modifier can be used only for instance or static fields, you cannot use readonly keyword for variables in the methods.
Hope this helps you...
Mark my answer as Brainliest. :-)
1. Constant are static by default
2. They must have a value at compilation-time
(you can have e.g. 3.14*2+5, but cannot call methods).
3. Could be declared within functions
4. These are copied into every assembly that uses them (every assembly gets a local copy of values).
Readonly
1. Must have set value, by the time constructor exits.
2. Are evaluated when instance is created.
3. You can use static modifier for readonly fields.
4. readonly modifier can be used with reference types.
5. readonly modifier can be used only for instance or static fields, you cannot use readonly keyword for variables in the methods.
Hope this helps you...
Mark my answer as Brainliest. :-)
Similar questions