Const string vs static readonly string

Source: Internet
Author: User

     STATIC_READONLY_STRING =    CONST_STRING = 
    

Compile and run:
C: \ Users \ de \ Desktop \ bao> csc/t: library/nologo ClassA1.cs

C: \ Users \ de \ Desktop \ bao> csc/r: ClassA1.dll/nologo ClassB1.cs

C: \ Users \ de \ Desktop \ bao> ClassB1
ClassA1.CONST _ STRING = CONST_STRING
ClassA1.STATIC _ READONLY_STRING = STATIC_READONLY_STRING

C: \ Users \ de \ Desktop \ bao>

Reedit ClassA1.cs

     STATIC_READONLY_STRING =    CONST_STRING = 

Recompile classa1.csand run classb1.exe:
C: \ Users \ de \ Desktop \ bao> csc/t: library/nologo ClassA1.cs


C: \ Users \ de \ Desktop \ bao> ClassB1
ClassA1.CONST _ STRING =
ClassA1.STATIC _ READONLY_STRING =


C: \ Users \ de \ Desktop \ bao>

????

Decompress classb1.exe

C: \ Users \ de \ Desktop \ bao> ildasm

C: \ Users \ de \ Desktop \ bao>

After classb1.exe is opened, view/metainfo/show! (Ctrl + M)

There is such a section at the bottom of metainfo:

User Strings
-------------------------------------------------------
70000001: (64) L "ClassA1.CONST _ STRING = {0} \ r \ nClassA1.STATIC _ READONLY_STRING = {1 }"
70000084: (12) L ""

And the IL code corresponding to ClassB1.ShowClassA1

. Method public hidebysig static void ShowClassA1 () cel managed
{
// Code size 23 (0x17)
. Maxstack 8
IL_0000: nop
IL_0001: ldstr "ClassA1.CONST _ STRING = {0} \ r \ nClassA1.STATIC _ READONL"
+ "Y_STRING = {1 }"
IL_0006:
IL_000b:
IL_0010: call void [mscorlib] System. Console: WriteLine (string,
Object,
Object)
IL_0015: nop
IL_0016: ret
} // End of method ClassB1: ShowClassA1

Conclusion:. NET string resident Mechanism

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.