1.Constants

is a symbol that has a never-changing value.  its value must be determinable at compile time.

 使用范围:

1.The compiler then saves the constant’s value in the assembly’s metadata.This means that you can define a constant only for types that your compiler considers primitive types.

In C#, the following types are primitives and can be used to define constants: Boolean, Char, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal, and String.

2.However, C# also allows you to define a constant variable of a non-primitive type if you set the value to null.

7.Constants and Fields

 

作用:

1.Because a constant value never changes, constants are always considered to be part of the defining type.

In other words, constants are always considered to be static members, not instance members.

2.Defining a constant causes the creation of metadata.

编译原理:

When code refers to a constant symbol, compilers look up the symbol in the metadata of the assembly that defines the constant, extract the constant’s value, and embed the value in the emitted Intermediate Language (IL) code.

特征:

1.Because a constant’s value is embedded directly in code, constants don’t require any memory to be allocated for them at run time.

2.In addition, you can’t get the address of a constant and you can’t pass a constant by reference.

can’t use constants if you need to have a value in one assembly picked up by another assembly at run time (instead of compile time). Instead, you can use readonly fields.

 

 

3.These constraints also mean that constants don’t have a good cross-assembly versioning story.

if you changes the constant value,only rebuilds the DLL assembly is not affected,it will have to be recompiled as well.

使用环境:

so you should use them only when you know that the value of a symbol will never change

示例说明1:

  7.Constants and Fields7.Constants and Fields

  7.Constants and Fields

  the application’s IL code

   assembly, the DLL assembly isn’t even loaded at run time and can be deleted from the disk

the compiler does not even add a reference to the DLL assembly in the application's metadata.

2.Fields

A field is a data member that holds an instance of a value type or a reference to a reference type.

修饰符:

  7.Constants and Fields

 

分类:

(nonstatic) fields.

the type is just-in-time (JIT)–compiled

  2.allocated when an instance of the type is constructed.

 特征:

  1.Because fields are stored in dynamic memory, their value can be obtained at run time only.

  solve the versioning problem that exists with constants.

  2.type, so you don’t have to restrict yourself to your compiler’s built-in primitive types.

  3.The CLR supports readonly fields and read/write fields.

meaning the field’s value might change multiple times as the code executes.

is first created). Compilers and verification ensure that readonly fields are not written to by any

method other than a constructor.

  but, reflection can be used to modify a readonly field.

注意:

  the reference that is immutable, not the object that the field refers to.

示例1:static readonly field

  7.Constants and Fields

  rebuild it.of the MaxEntriesInList field out of the dynamic memory allocated for it.

  changes the 50 to 1000 and rebuilds the assembly,pick up the new value: 1000.strongly named and the versioning policy of the application is such that the CLR loads this new version.

示例2:how to define a readonly static field that is associated with the type itself, as well as read/write static fields and readonly and read/write instance fields

 

  7.Constants and Fields

 

相关文章: