程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> const string vs static readonly string

const string vs static readonly string

編輯:C#入門知識

     STATIC_READONLY_STRING =    CONST_STRING = 
    

編譯並運行:
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>

重新編輯ClassA1.cs

     STATIC_READONLY_STRING =    CONST_STRING = 

重新編譯ClassA1.cs,並運行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>

????

反編譯打開ClassB1.exe

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

C:\Users\de\Desktop\bao>

打開ClassB1.exe後,view/metainfo/show! (Ctrl + M)

在metainfo的最底部有這麼一節:

User Strings
-------------------------------------------------------
70000001 : (64) L"ClassA1.CONST_STRING = {0}\r\nClassA1.STATIC_READONLY_STRING = {1}"
70000084 : (12) L""

以及ClassB1.ShowClassA1對應的IL代碼

.method public hidebysig static void  ShowClassA1() cil 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

結束語:.NET字符串駐留機制

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved