Interviews4 answers
What's difference between constants and static readonly?
4 Answers
Constants : Constant value is initialized at compile time.
Const int a=10;
static Readonly : Static Readonly Initialize in static constructor only.
Pramod VermaPosted Jan 24, 2015, 8:46 AM EST
readonly: This is the Runtime constant. We can initialize the values once through constructor(while running).
constant: Constant value is initialized at compile time. We can not initialize the constant values at compile time.
sudhakar sakthivelPosted Sep 1, 2009, 5:03 AM EDT
A const must be initialized at the time of its creation. A readonly field can be assigned to once in the class constructor allowing you to pass in the value at run-time. Declaring fields as const protects both you and other programmers from accidentally changing the value of the field. Also note that with const fields, the compiler performs some optimization by not declaring any stack space for the field. The readonly keyword is similar to const, with two exceptions. First, the storage of a readonly field is the same as a regular read-write field, and thus there is no performance benefit. Secondly, readonly fields can be initialized in the constructor of the containing class.
prashanthPosted Jun 5, 2007, 1:44 AM EDT
Constants :- it's value set at compile time and support value types
Static readonly:it's value set at run time and are able to hold refrence types
shujat_ch2004Posted Jun 2, 2007, 5:46 AM EDT
