What are the benefits of having a member variable declared as read only? Is it just protecting against someone changing its value during the lifecycle of the class or does using this keyword result in any speed or efficiency improvements?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
readonlykeyword is used to declare a member variable a constant, but allows the value to be calculated at runtime. This differs from a constant declared with theconstmodifier, which must have its value set at compile time. Usingreadonlyyou can set the value of the field either in the declaration, or in the constructor of the object that the field is a member of.Also use it if you don’t want to have to recompile external DLLs that reference the constant (since it gets replaced at compile time).