".NET framework program design" Chapter 8 constant and field

xiaoxiao2021-03-06  97

Chapter 8 Constant and Fields

First, constant

1. The type that can be defined as a constant is:

2 primitive type: Boolean, Char, Byte, Sbyte, Decimal, Int16, Int32, Uint16, Uint32, Int64, Uint64, Single, Double

2 string: String

2 enumeration type

2, the constant is embedded directly into the IL code after compiling, so the constant in a module cannot be obtained by another module at runtime. The former is modified to constant can't be perceived by another module, so I want to run at runtime. Get "unchanged value" should use read-only fields

Second, field

1, fields (including static, instances, or read-only fields) allocated memory at runtime

2, the read-only field can only be value value in the constructor (or directly assigned the value when the declaration is declared, and the other places are not allowed, which is actually assigned in the constructor), and the static read-only field is in the type constructor. Assignment, the instance read-only field is in the example constructor.

3, static read-only fields can only use type name access, not to use instance references

Third, the difference between static read-only fields and constants:

1. Constant directly compile time embed IL code, can not read again at runtime, static read-only fields can be reread at runtime

2, constants can only assign values ​​at the statement, while static read-only fields can assign values ​​in the constructor.

转载请注明原文地址:https://www.9cbs.com/read-122408.html

New Post(0)