[C #] Summary of fields

Source: Internet
Author: User

Frontier:

A field is a data member that contains an instance of the value type or a reference type.

Body:

CLR supports the type (static) field and instance (non-static) field. For type fields, the dynamic memory used to hold field data is allocated in the type object, and the type object is created when the type is recorded in an appdomain. So when will the type be loaded into an appdomain? This is usually the first JIT compilation by referencing any method of this type. For instance fields, the memory used to hold field data is allocated when constructing an instance of the type.

Because fields are stored in the dynamic memory, their values can be obtained at runtime. The field also solves the version control problem of constants. In addition, fields can be any data type and do not have to be limited to primitive types built in the compiler as constants. So what are primitive types (INT, unit, and so on )?

Primitive type: the data type directly supported by the compiler. If there is no primitive type, we should be like this when declaring an integer.

Int32 a=new Int32;

Fortunately, with the primitive type, we can directly use the following method, which we often use during programming:

int a=0;

Again, for example, string and string. At first, I was very confused about the two types. When should I use them? But after reading the explanations of the primitive types, you will understand, since the string of C # is directly mapped to the string (FCL type), there is no difference between the two. Both can be used.

 

CLR supports readonly and read and write fields. Most fields are read and write fields, which means that the field value can be changed multiple times during code execution. However, the readonly field can only be written in one constructor method. (This constructor method can only be called once, that is, when the object is first created). The compiler and verification mechanism ensure that the readonly field is not written by methods other than the constructor. However, reflection can be used for modification.

Now we use a static readonly field to correct the version control problem:

1 public sealed class someType2 {3       public static readonly int MaxList=50;   4 }

This is the only one that needs to be modified and the application code does not need to be modified. However, to observe new behaviors, you must generate a new one. When the main method of the application is run, CLR will load it to the DLL assembly, and extract the value of the maxlist field from the dynamic memory allocated to him 50.

Assume that the developer of the DLL Assembly changes 50 to 100 and regenerate the assembly. When the application code is re-executed, it will automatically extract the new value of the field 100. In this case, the program does not need to be re-generated and can be directly run. Note that the new version of the DLL assembly is not named.

The following demonstrates how to define a readonly static field and a read/write field associated with the type. The read/write static fields are also defined.

Class servicelocator {// static read-only field. When this class is initialized at runtime, its value is stored in the memory public static readonly random = new random (); private Static int numberwrites = 0; Public readonly string pathname = "Unity"; private filestream FS; Public servicelocator (string pathname) {// you can modify the read-only field this in the constructor. pathname = pathname;} Public String do () {numberwrites + = 1; return pathname ;}}

In the code above, many fields are initialized inline (that is, they are directly assigned values in the Code for initialization, rather than through constructors ), C # This method can be used to initialize constants, read/write fields, and read-only fields of a class. C # In fact, fields are initialized in the constructor. The Inline initialization of fields is just a Syntactic simplification.

Note: When a field is of the reference type and declared as read-only, the reference itself, rather than the referenced object (that is, the value allocated in the heap), cannot be changed ).

  

Public static readonly char [] inva = new char [] {'A', 'B', 'C'}; Public void test () {inva [0] = 'V'; // you can change the referenced object, that is, change the address inva = new char [] {'x ', 'y', 'z'}; // The reference itself cannot be changed}

 

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.