[CLR via C #] 7. constants and fields

Source: Internet
Author: User

7.1 Constants

A constant is a special value that never changes.

When defining a constant, its value must be determined during compilation. After confirmation, the compiler saves the constant value to the metadata of the Assembly. This means that constants can only be defined for the primitive type recognized by the compiler. C # Allows defining a non-primitive constant variable (constant variable), but this value should be set to null.
        SomeType Empty = 

When the code references a constant symbol, the compiler searches for the symbol in the metadata of the Assembly that defines the constant, extracts the value of the constant, and embeds the value in the generated IL code. Since the constant value is directly embedded in the code, you do not need to allocate additional memory for the constant during runtime. In addition, the constant address cannot be obtained, or the constant cannot be transferred as a reference. Based on the previous article, if assembly A only depends on constants in assembly B, after compilation, assembly A will not be affected even if assembly B is deleted, it is also a constant value that can be defined in the B program set. Field 7.2A field is a data member that contains an instance of the value type or a reference to a reference type. The following table summarizes the modifiers applied to fields:
CLR terminology C # terminology Description
Static Static This field is part of the type state, not part of the object state.
Instance Default This field is associated with an instance of the type, rather than the type itself.
InitOnly ReadOnly This field can only be written by code in one constructor Method
Volatile Volatile When you see the code accessing this field, the compiler, CLR, or hardware will not execute some optimization measures for "thread unsafe ".
For instance fields, the dynamic memory used to hold field data is allocated when constructing an instance. Because fields are stored in the dynamic memory, all their values can be obtained at runtime. The field also solves the version control problem of constants. In addition, fields can be of any data type. CLR supports readonly and read/write fields. Most fields are read/write fields, which means that the fields can be changed multiple times during code execution. However, the readonly field can only be written in one constructor method. (If you have any questions, can I directly define the value of readonly? For example, private readonly int ss = 123; Do not write data from the constructor method? This issue will be mentioned later ). Note: You can use reflection to modify the readonly field. The following describes how to define a readonly static field associated with the type and a read/write static field. The read/wite static fields and readonly and read/write instance fields are also defined.
         Random s_random =       Int32 s_numberOfWrites =       String Pathname =             .Pathname =        s_numberOfWrites = s_numberOfWrites +         

Note: When a field is of the reference type and marked as readonly, the reference cannot be changed, not the value referenced by the field.
             Char[] InvalidChars =  Char[] { , ,              AType.InvalidChars[] = ] = ] = 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.