This article references from: http://book.51cto.com/art/201109/292329.htm
Many beginners cannot tell the use cases of readonly and Const. In my opinion, there is only one reason to use const, that is, efficiency. However, in most cases, "efficiency" is not so high, so I prefer to use readonly, because readonly givesCodeMore flexibility. The essential differences between const and readonly are as follows:
Const is a constant during the compilation period, and readonly is a runtime constant.
Const can only modify the primitive type, enumeration type, or string type. readonly has no restrictions.
Regarding the first difference, because const is a constant during the compilation period, it is static. You cannot manually add a static modifier for const. The following code will make the compilation fail:
- Static const intConstvalue=100; // The constant "constvalue" cannot be marked as static
The const variable is highly efficient because, after compilation by the compiler, the actual value corresponding to the const variable will be used to reference the const variable in the code, for example:
- Console. writeline (constvalue );
It is consistent with the Il code generated by the following code:
- Console. writeline (100 );
The readonly variable is a runtime variable, and its value assignment occurs at runtime. The full significance of readonly is that it cannot be changed after it is assigned a value for the first time during runtime. Of course, "cannot be changed" is divided into two meanings:
1) For value type variables, the value itself cannot be changed (readonly, read-only ).
2) for reference type variables, the reference itself (equivalent to a pointer) cannot be changed.
Let's look at the value type variable and see the following code:
- Class sample
- {
- Public readonly int readonlyvalue;
-
- Public sample (INT value)
- {
- Readonlyvalue=Value;
- }
- }
The readonlyvalue of the sample instance cannot be changed after being assigned a value in the constructor. The following code will not be compiled:
- SampleSample=NewSamples (200 );
- Sample. readonlyvalue=300; // Cannot read-only words
- // Segment assignment (except for the specified constructor or variable initial value)
For reference type variables, see the following code:
- Class sample2
- {
- Public readonly student readonlyvalue;
-
- Public sample2 (student value)
- {
- Readonlyvalue=Value;
- }
- }
The readonlyvalue of sample2 is a reference type variable. After a value is assigned, the variable cannot point to any other student instance. Therefore, the following code will not compile and pass:
- Sample2Sample2=NewSample2 (new student (){Age=10});
- Sample2.readonlyvalue=NewStudent (){Age=20};
- // The value of the read-only field cannot be assigned (except for the specified constructors or variable initial values)
However, as we have mentioned before, the reference itself cannot be changed, but the value of the instance referred to by the reference can be changed. The following code will be allowed:
- Sample2Sample2=NewSample2 (new student (){Age=10});
- Sample2.readonlyvalue. Age=20;
Readonly indicates that the runtime has an important role, that is, you can specify a readonly variable for each class instance. Taking the sample class as an example, you can generate multiple instances at runtime, and at the same time, you can generate your own readonly variable for each instance, as shown below:
- SampleSample1=NewSamples (100 );
- SampleSample2=NewSamples (200 );
- SampleSample3=NewSamples (300 );
This is the runtime meaning represented by the readonly variable, which is also the flexibility of the readonly variable.
Note that it is incorrect that the readonly variable cannot be assigned again. The following code indicates that it can be changed:
-
- Class Program
-
- {
-
- Static void main (string [] ARGs)
- {
-
- SampleSample=NewSamples (200 );
-
- Console. writeline (sample. readonlyvalue );
-
- }
-
- }
-
-
-
- Class sample
-
- {
-
- Public readonly intReadonlyvalue=100;
-
-
- Public sample (INT value)
-
- {
-
- Readonlyvalue=Value;
-
- }
-
- }
Readonlyvalue is first assigned to 100 in the specified item of the initial value (also called the initialization device), and then to 200 in the constructor. In fact, the initialization tool should be understood as part of the constructor, which is actually a syntactic sugar. In the constructor, we can indeed assign values to readonly multiple times.