What is the difference between constant and read-only in c#?(Interview Question) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the difference between constant and read-only in c#?(Interview Question)

15th May 2019, 1:37 AM
$@G@®️
$@G@®️ - avatar
2 Answers
+ 1
ReadOnly :The value will be initialized only once from the constructor of the class. The difference is that the value of a static readonly field is set at run time, so it can have a different value for different executions of the program. However, the value of aconst field is set to a compile timeconstant.
15th May 2019, 2:46 AM
Janbaz Khan
Janbaz Khan - avatar
+ 1
Constants: 1. Constants can be assigned values only at the time of declaration 2. Constant variables have to be accessed using "Classname.VariableName" 3. Constants are known at compile time Read Only: 1. Read only variables can be assigned values either at runtime or at the time of instance initialization via constructor 2. Read only variables have to be accessed using the "InstanceName.VariableName" 3. Read only variables are known at run time.
5th Jun 2019, 2:06 PM
$@G@®️
$@G@®️ - avatar