What is the difference between readonly and const
Answers were Sorted based on User's Feedback
Answer / sabina
both meant for constant values. a const field can only be
initialized at the declaretion of the field. a read only
field can be initialized either in initialization or in
constructer. therefore , reaadonly field can have different
values depending on the constructure used
also a const field is compile time constant , readonly
field can be used for runtime constants
Is This Answer Correct ? | 18 Yes | 2 No |
Answer / chandan kumar
The read only keyword is different from the const keyword. A
const field can only be initialized at the declaration of
the field. A read only field can be initialized either at
the declaration or in a constructor. Therefore, read only
fields can have different values depending on the
constructor used. Also, while a const field is a
compile-time constant, the read only field can be used for
runtime constants as in the
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / vishnu
a read only
field can be initialized either in initialization or in
constructor
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / porchelvi.a
Constant:
--------
we have to initialize at the declaration itself
we could not change the value of constant variable.
Read only:
----------
we can not initialize the Read only variable during
declaration.we can initialize it in constructor only.after
that we could not change the value.Read noly is instance
based variable.
Is This Answer Correct ? | 1 Yes | 3 No |
What is Bubble Event ?
without modifying source code if we compile again, will it be generated MSIL again?
Use of Checked and UnChecked operators?
What is garbage collection? How to force garbage collector to run?
What is difference between hashtable and dictionary in c#?
Can we have multiple constructors in a class c#?
What?s a delegate?
How do you comment in c#?
2. What happened when BO object has been called?
What is icomparer in c#?
Is vs as c#?
What is difference between virtual and override in c#?