[C # advanced series] 07 constants and fields,

Source: Internet
Author: User

[C # advanced series] 07 constants and fields,

Constant

Constants are always regarded as static members.

Constants can be not limited to primitive types, but must be initialized to null. (I think this is the same as what I do not know. I have automatically ignored it from my mind. Many times, in my eyes, there is no difference between difficult code and junk code. I hate to have simple and easy-to-understand solutions. programmers who have to dazzle some uncommon and useless technologies always like to use this method to make my brother unable to get off work on time)

During compilation, the constant values are directly extracted and embedded into the IL code. Therefore, no space needs to be allocated during runtime.

Field

Previously, we have discussed that CLR supports static fields and instance fields.

Static fields are placed in the type object. The type object is created when the type is loaded to an AppDomain (usually any method that references this type is used for JIT compilation for the first time ),

The instance field is placed in the instance object.

In addition to the default instance field and static field, the modifier of the field also has readonly to indicate read-only fields.

As we all know, read-only fields can only be written to or initialized inline by one constructor method (that is, values are directly assigned when declared in the Code, rather than using constructor ).

In fact, inline initialization uses a constructor to initialize fields. inline Initialization is just a syntax simplification. (In fact, there are still some performance issues to consider during inline initialization. Write it in the next chapter)

Of course, there are other ways to do this. For example, if you use reflection, it is a pity to modify the readonly field.

There is also a modifier that is rarely used, volatile (the document is translated as variable and the author expresses it as variable, because this field can be modified by multiple threads). To be honest, I did not know such a modifier before I read this book. ⊙ ‖ ↓

When a field is modified with volatile, the compiler, CLR, and hardware will not perform "thread-unsafe" optimization measures on the code accessing this field. (This is mentioned in the thread synchronization behind this book. I haven't seen it yet. Please write it later)

 

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.