Qus:    Differentiate between constants and read-only variables.
Oct 22, 2020 13:37 DotNet 2 Answers Views: 1610 RITA
Prev Next
Answers (2)
ANANYA Oct 23, 2020 07:35
Answer:   Constants
Evaluated at compile time
Support only value type variables
They are used when the value is not changing at compile time
Cannot be initialized at the time of declaration or in a constructor

Read-only Variables
Evaluated at run-time
They can hold the reference type variables
Used when the actual value is unknown before the run-time
Can be initialized at the time of declaration or in a constructor

PARTH Oct 23, 2020 13:26
Answer:   1) const fields has to be initialized while declaration only, while readonly fields can be initialized at declaration or in the constructor.
2) const variables can declared in methods ,while readonly fields cannot be declared in methods.
3) const fields cannot be used with static modifier, while readonly fields can be used with static modifier.
4) A const field is a compile-time constant, the readonly field can be used for run time constants.

Post Your Answer
Guest User

Not sure what solution is right for you?

Choose the right one for you.
Get the help of the experts and find a solution that best suits your needs.


Let`s Connect