共用方式為


initonly

initonly indicates that variable assignment can only occur as part of the declaration or in a static constructor in the same class.

initonly is a context-sensitive keyword; see Context-Sensitive Keywords for more information.

Example

// mcpp_initonly.cpp
// compile with: /clr /c
ref struct Y1 {
   initonly
   static int staticConst1;

   initonly
   static int staticConst2 = 0;

   static Y1() {
      staticConst1 = 0;
   }
};

See Also

Reference

Classes and Structs (Managed)