The difference between the const, static, and readyonly keyword modifiers for C # is illustrated by a simple example:
1 //SomeType.cs2 3 usingSystem;4 5 Public Sealed classSomeType6 {7 PublicInt32 Id =1;8 9 Public ConstInt32 ConstID = -;//IL. Field public static literal Int32 ConstID = Int32 (0x00000032)Ten One Public StaticInt32 Staticid = +;//IL. Field public static Int32 Staticid A - Public ReadOnlyInt32 Readonlyid = -;//IL. Field Public initonly int32 Readonlyid - the Public ReadOnlyint32[] Readonlyarrayid =Newint32[]{1,2,3};//The Readonlyarrayid itself cannot write again, but the object pointed to can be changed - - Public Static ReadOnlyInt32 Staticreadonlyid = the;//IL. Field public static initonly int32 Staticreadonlyid - + PublicSomeType () - { +Readonlyid =20001; A } at}
1 //TestSomeType.cs2 3 usingSystem;4 5 Public classTestconst6 {7 Public Static voidMain (string[] args)8 {9SomeType SomeType =NewSomeType ();Ten One //test1 A intresult = Sometype.id + Sometype.constid + sometype.staticid + Sometype.readonlyid +Sometype.staticreadonlyid; - - //test2 thesometype.readonlyarrayid[1] =4; - -Console.WriteLine (sometype.readonlyarrayid[1]);//Output 4 - + //test3 -Console.WriteLine ("ConstID:" + Sometype.constid);// + A Console.WriteLine ("Staticid:" + sometype.staticid); at } -}
I. Conclusion
1. ReadOnly
(1) When modifying a member variable, the variable can only be assigned through a variable definition or early construction method.
(2) A modified member variable is an instance field that is allocated when the memory is an instance of the constructor type.
(3) When a modified member variable is a reference type, immutable is a reference, and the object to which the reference is directed can be changed.
2. Static
(1) The modified member variable is a type resource, memory is allocated in the type object, and the specific instance is irrelevant.
3. Const
(1) Compiled into IL code is modified by the static literal, this shows that the const modified variable is ultimately expressed as a static type is also literal decorated (literal modified variable must be assigned at the time of the variable declaration, When the compiler compiles the IL code, the value of the variable is inserted directly into the reference variable to replace the previous variable, so that the program does not need to allocate memory for this variable at run time.
Two. The difference between const and static
1. Compiling SomeType.cs and TestSomeType.cs
////addmodule:sometype.netmodule
Run TestSomeType.exe to see the output in Test3 as:
constid:1001000
Now change the values of SomeType.cs type ConstID and Staticid to 50, 500, and then regenerate Sometype.netmodule
Then run TestSomeType.exe again to see that the output in Test3 is:
constid:100500
The result is that only staticid values are referenced by the new value. Then take a look at the IL Code of the main function of the TestSomeType.cs class:
You can see the four tags. Tokens (1) and (3) are representations of ConstID compiled into IL Code, tokens (2) and (4) are representations of staticid compiled into IL code.
For ConstID we can find that ConstID has actually been replaced with its corresponding value, so changing the SomeType.cs ConstID recompilation will not affect the compiled Testsometype assembly.
The STATICID variable indicates the information of the module in which the CLR loads its corresponding netmodule to get the value of Staticid when the program is running. So change the value of the SomaeType.cs staticid and recompile to get the most recent value in the Testsometype assembly.
C # const, static, and readonly keyword understanding