+ 2
What is the difference between constant and read-only in c#?(Interview Question)
2 Respostas
+ 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.
+ 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.