I am eager to know the difference between a const variable and a static variable.
Is a const variable also always static? What is the difference between them?
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.
constfields can only hold value types orSystem.String. They must be immutable and resolvable at compile-time.static readonlyfields can and generally do hold reference types, which (other than strings) can only be created at runtime. These can (but shouldn’t) be mutable types; the only thing that cannot change is the reference itself.If you need to maintain a “constant” set of instances that are reference types, you generally do it with a set of
public static readonlyfields, such as the members of System.Drawing.SystemColors.Last but not least, initialization of a
readonlyfield can be deferred until the execution of a constructor, which means that it even though it can only be written to once, it does not always have to be initialized with the exact same value. True constants declared withconstcan only ever have a single value (specified at compile time).