Interviews3 answers
What’s the difference between const and readonly?
3 Answers
Constant is we doesn't have change value. Read-only same as constant but we should be constructor value change at run time.
Mageshwaran RPosted Nov 29, 2018, 5:20 AM EST
Const has fixed value
You have intialise same time
Mukesh KumarPosted Aug 31, 2017, 5:47 AM EDT
Readonly fields are delayed initalized constants. However they have one more thing different is that; When we declare a field as const it is treated as a static field. where as the Readonly fields are treated as normal class variables.const keyword used ,when u want's value constant at compile time but in case of readonly ,value constant at run timeForm the use point of view if we want a field that can have differnet values between differnet objects of same class, however the value of the field should not change for the life span of object; We should choose the Read Only fields rather than constants.Since the constants have the same value accross all the objects of the same class; they are treated as static.
Samir BhogaytaPosted Jun 25, 2016, 12:05 AM EDT


