???????? concatenation ???????? ??? ????? C# ??????? ?? ??? ???? ????

???? ?????? ???? ??????
???? ID: 306822 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ??? ?? ??? ???? ???? ????? ?? ????? ?? ?? ???StringBuilder???? ???????? concatenation ??????? ?? ??? .NET Framework ??? ?????????? ????????? ??? (??????, ???????? ??? ?? ????-????? ?? ??? ????????? ????? ?? ???)? ?? ?? ???????? ??? ?? ?????? ????? ?? ?? ?????? accustomed C/C++ ???????? manipulation ??????? ?? ??? ?? ?? ???? ?? ??? ??? challenges poses ???

.NET Framework ??? ?????????? ?? ?????

???????? concatenation ?? ??? ????? ???? ?? ??? ?? ?????strcat()??? ????? C/C++ ???? ??? ???? ?? ????????? ???????? ???? ?? ??? ??? ?? ??? ???? ???? ?????? ???? ?? ??? ??? .NET Framework ??? ?? ???????? immutable ??; ?? ????? ?? ??????? ???? ?? ??? C# + concatenation ?????? ?? ???? ?? ???????? ?? ?? ????? ??????????? ?? ?? ?? concatenates ??? ?? ???? ?????? ?????

???????, .NET Framework ??? ????? ???StringBuilder???? ?? ???????? concatenation ?? ??? ????????? ???? ??? ??? ?? C/C++, ??? ?? ???? ???? ?? ????? ?? ??? ?? ?? ??? ?? ???????? ??? ?? (??? ?????? ??) ???? ???? growing ?? ????? ?? ???? ?? ??? ???????? ?? ??? ??? ?? ??? ?? ?????? ????? ??? ????????? ???? ?? ?? ???? ??? ????? ????????? ?? ?? ?????StringBuilder???? ?? concatenation ???? ?? ??? ???????? ?? ????? ???? ???

????? ?? ???? ???????? ????????? ?????

  1. Visual Studio ??????? ????, ?? ???? ??? ?? ??? ????? C# ????? ????????? ??????
  2. ????? ??? + = concatenation ?????? ?? ????? ???? ?? ??StringBuilder???? ?? 30 ?? 5,000 concatenations ??? ?? ??? ???????? ???? ??? ????? ????????? ???? ?? ??? ?? ??? ???????
    const int sLen=30, Loops=5000;
    DateTime sTime, eTime;
    int i;
    string sSource = new String('X', sLen);
    string sDest = "";
    // 
    // Time string concatenation.
    // 
    sTime = DateTime.Now;
    for(i=0;i<Loops;i++) sDest += sSource;
    eTime = DateTime.Now;
    Console.WriteLine("Concatenation took " + (eTime - sTime).TotalSeconds + " seconds.");
    // 
    // Time StringBuilder.
    // 
    sTime = DateTime.Now;
    System.Text.StringBuilder sb = new System.Text.StringBuilder((int)(sLen * Loops * 1.1));
    for(i=0;i<Loops;i++) sb.Append(sSource);
    sDest = sb.ToString();
    eTime = DateTime.Now;
    Console.WriteLine("String Builder took " + (eTime - sTime).TotalSeconds + " seconds.");
    // 
    // Make the console window stay open
    // so that you can see the results when running from the IDE.
    // 
    Console.WriteLine();
    Console.Write("Press Enter to finish ... ");
    Console.Read();
    					
  3. ????????? ?? ??????? ?????? ???? ?? ?? ?? ????????? ?? ????? ?? ??? F5 ?????? ????? ????? ??? ????? ?? ???? ?????? ????????? ???? ?????:
    Concatenation 6.208928 ????? ???? ??? ???
    ???????? ?????? 0 ????? ???? ??? ???

    ?????? ???? ?? ??? ENTER ?????...
  4. ????????? ?? ??? ?? ??? ???? ?? ??? ?? ?????? ????? ?? ??? ???? ?? ??? ENTER ??????

?????? ??????

  • ?????????? ????, ??? ???? ASPX ??? ??????? ?? ???? ????????? ??? ?? ??? ???, ????? ?? ??? ???? ??? ??? ?? ?? ?????? ???? ???? ?? ??????? ??? ???? ??? ??? concatenation ?? ???? overhead avoiding ???? ?? ????? ??StringBuilder, ?? ???? ?? ?? ?????? ?? ??????? ?? ????Response.Writemethod or the appropriate method for the stream in question.
  • Try to reuse the existingStringBuilderclass rather than reallocate each time you need one. This limits the growth of the heap and reduces garbage collection. In either case, using theStringBuilderclass makes more efficient use of the heap than using the + operator.

??????

TheStringBuilderclass contains many other methods for in-place string manipulation that are not described in this article. For more information, search for "StringBuilder" in the Online Help.

???

???? ID: 306822 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2008 Express Edition
??????: 
kbsweptvs2008 kbhowtomaster kbperformance kbmt KB306822 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:306822

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com