Differences between String and StringBuilder in asp. NET

Source: Internet
Author: User

When many strings are connected together in a loop, using the stringbuilder class can improve performance. Why? The biggest difference is that their memory allocation mechanisms are different.

Memory Allocation:

String

The string object cannot be changed. Each time you use one of the methods in the string class or perform operations (such as assignment and concatenation), You must create a new string object in the memory, in this case, you need to allocate a new space for the new object.
Stringbuilder

The int capacity attribute of the stringbuilder instance, which represents the total number of strings physically allocated for storing strings in memory. This number is the capacity of the current instance. When the result of the string operation is greater than the current capacity, the value of this attribute automatically increases. The increase is two times of the current value.

Note: The capacity attribute of variable collection classes such as arraylist in. net framework is similar to this automatic allocation mechanism.
Okay. Now, when it comes to string, we will introduce the reference and comparison of string objects.

Guess what results will the following code output ??

Public void test1 ()
{
String a = "abcd ";
String B = "abcd ";
/**
* Object. referenceequals (object obja, object objb );
* Returned results:
* If the obja instance is the same as the objb instance, or if both instances are empty references, the value is true; otherwise, the value is false.
****/
Console. writeline (object. referenceequals (a, B ));
}


This code will output

True

If you don't believe it, you can verify it by yourself, because the system has allocated memory for this string instance when "abcd" is defined in your code, here, "abcd" is stored in the memory as an instance object, and this string is used below, so the system directly assigns its reference to the B variable.

 

Next, let's guess what the output will be?

 

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 code
. Method public hidebysig static void main (string [] args) cel managed
{
. Entrypoint
. Maxstack 2
. Locals init (
[0] string,
[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
}

 
It can be seen that the compiler has linked the string before compiling into the il code, so it will not affect our running results.
Above code output
True
True
True

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.