asp.NET 中String 和StringBuilder 區別詳解

來源:互聯網
上載者:User

當在一個迴圈中將許多字串串連在一起時,使用 stringbuilder 類可以提升效能,為什麼呢?最大區別在於他們的記憶體配置機制不同。

記憶體配置:

string

string 對象是不可改變的。每次使用 string 類中的方法之一或進行運算時(如賦值、拼接等)時,都要在記憶體中建立一個新的字串對象,這就需要為該新對象分配新的空間
stringbuilder

stringbuilder 執行個體的 int capacity 屬性,它表示記憶體中為儲存字串而物理分配的字串總數。該數字為當前執行個體的容量。當字串操作的結果大於當前的容量時,該屬性的值會自動成長。增長的幅度為當前數值的2倍。

註: .net framework中可變集合類如arraylist 的capacity 屬性也類似這種自動分配機制
ok 既然說到string 順便介紹下string對象的引用比較問題

猜猜下面這段代碼將會輸出什麼結果??

     public void test1()
        {
            string a = "abcd";
            string b = "abcd";
            /**
             * object.referenceequals(object obja, object objb);
             * 返回結果:
             * 如果 obja 是與 objb 相同的執行個體,或者如果二者都為空白引用,則為 true;否則為 false。
             * ***/
            console.writeline(object.referenceequals(a,b));
        }


這段代碼將輸出

true

是的不信你可以自己驗證一下,因為在你代碼中定義"abcd" 的時候系統就已經為這個字串執行個體分配了記憶體,這裡的"abcd"將作為一個執行個體Object Storage Service在記憶體中,在下方又用到了這個字串,所以系統直接將它的引用賦值給了b變數

 

再來一段,猜猜這個將會輸出什麼結果?

 

public static void main(string[] args)
        {             string a = "abeqwec";
            string b = "abeqwe" + "c";
            string c = "abeqwec";
            console.writeline(object.referenceequals(a,c));
            console.writeline(object.referenceequals(a,b));
            console.writeline(object.referenceequals(b,c));
            console.readline();
        }
 
 
il 代碼
.method public hidebysig static void main(string[] args) cil managed
{
    .entrypoint
    .maxstack 2
    .locals init (
        [0] string a,
        [1] string b,
        [2] string c)
    l_0000: nop
    l_0001: ldstr "abeqwec"
    l_0006: stloc.0
    l_0007: ldstr "abeqwec"
    l_000c: stloc.1
    l_000d: ldstr "abeqwec"
    l_0012: stloc.2
    l_0013: ldloc.0
    l_0014: ldloc.2
    l_0015: call bool [mscorlib]system.object::referenceequals(object, object)
    l_001a: call void [mscorlib]system.console::writeline(bool)
    l_001f: nop
    l_0020: ldloc.0
    l_0021: ldloc.1
    l_0022: call bool [mscorlib]system.object::referenceequals(object, object)
    l_0027: call void [mscorlib]system.console::writeline(bool)
    l_002c: nop
    l_002d: ldloc.1
    l_002e: ldloc.2
    l_002f: call bool [mscorlib]system.object::referenceequals(object, object)
    l_0034: call void [mscorlib]system.console::writeline(bool)
    l_0039: nop
    l_003a: call string [mscorlib]system.console::readline()
    l_003f: pop
    l_0040: ret
}

 
由此可見在進行編譯為il代碼之前,編譯器已經將字串做了連結,所以對我們的運行結果將不會有影響
以上代碼 輸出
true 
true
true

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.