Designer handbags from All Handbag Fashion

December 26, 2011

. Net in constant fields const

Filed under: Fashion — Tags: — admin @ 12:59 pm

June 17, 2010
C #, when using the constant symbol const, the compiler first define the constants from the module metadata to find the symbol, and directly remove the constant value, then The embedded into the compiled IL code generated, so the constant in the run without allocating any memory, of course, the address will not be able to obtain a constant and can not use references.
the following code: to compile ConstTest.dll file and referenced in the following code in this ConstTest.dll file. Main.exe compile and run this program, the resulting output of 1000. After the bin folder ConstTest.dll references deleted, run directly Main.exe file, the program runs properly, the result output 1000.
If ConstTest re-defined as: 1: publicclass ConstTest 2: {3: / / only in the definition statement 4: publicconstint ConstInt = 1000; 5: 6: publicreadonlyint ReadOnlyInt = 100; 7: publicstaticint StaticInt = 150; 8: 9: public ConstTest () 10: {11: ReadOnlyInt = 101; 12: StaticInt = 151; 13:} 14: 15: / / static front can not add modifier 16: static ConstTest () 17 : {18: / / initialize static variables here only 19: StaticInt = 152; 20:} 21:}
recompiled into ConstTest.dll to add this reference to the calling program after the Main , and then compile the calling program to generate new Main.exe, even if the file again delete ConstTest.dll, Main.exe operating normally, the result output 1000.
changes in the Main program are as follows: 1: class Program 2: {3: publicstaticvoid Main (string [] args) 4: {5: 6: Console.WriteLine (ConstTest.ConstInt); / / Output 1000 7:8: Console.WriteLine (ConstTest.StaticInt); / / output 152 9:10: ConstTest mc = new ConstTest (); 11: Console.WriteLine (ConstTest.StaticInt); / / output 15112:} 13: }
recompile the Main program and, if you then ConstTest.dll deleted, an error.
so you can see, if some of the project refers to a ConstTest.dll, because if you later change the value of the constant changes ConstInt Even though a reference to re-compile the ConstTest.dll, can not change Main.exe data (can ConstInt value to another value, and then compiled ConstTest.dll copied to the bin folder Main.exe try), then, can only add ConstTest.dll reference, and re-compile the Main process the job.

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

www.ybhaiwai.cn www.cms52.cn