全部博文(20)
分类:
2009-11-02 15:52:07
To define a constant field, simply apply the const modifier and initialize it to some value. A constant field must be initialized to another constant value, either a literal or a constant variable. Of course, once initialized, a constant field value can never change.
The compiler handles constants by "hard coding" the value into the CIL(Common Intermediate Language, it is also referred to IL or MSIL). Also note that constant members are implicitly static.
Use the readonly modifier to define a field that need to be initialized with a non-constant value but must never change after initialization.
The value of a readonly field is determined dynamically at runtime. In contrast, the value of a constant field is hardcoded into the CIL at compile time.
Readonly fields are instance fields, whereas constant fields are implicitly static.