Related blog address:
Http://www.cnblogs.com/artech/archive/2007/03/04/663728.aspxhttp://www.cnblogs.com/ASPNET2008/archive/2009/06/28/1512807.html below is a personal exercise: Code
// String detention exercise by mcjeremy
// When S1 is declared, this string does not exist in the detention pool, so it is placed in
// String. isintered (S1)
String S1 = "ABC123 ";
// When S2 is declared, no memory is allocated because the string already exists in the detention pool.
// S2 and S1 point to the same reference
String S2 = "ABC123 ";
// The actual call behind S3 is Concat, so it is dynamically created.
// Therefore, a new memory is allocated.
String S3 = S1 + S2;
// When S4 is created, the compiler will optimize it and call ldstr. Therefore, it will find that the detention pool has
// If ABC123 exists, no memory is allocated. At this time, S1, S2, and S4 all point to the same reference.
String S4 = "ABC" + "123 ";
String S5 = "123 ";
// When S6 is created, Concat is actually called. Therefore, it is created dynamically.
// Therefore, a new memory is allocated.
String S6 = "ABC" + S5;
Console. writeline (object. referenceequals (S1, S2); // true
Console. writeline (object. referenceequals (S1, S3); // false
Console. writeline (object. referenceequals (S1, S4); // true
Console. writeline (object. referenceequals (S1, S6); // false
Console. writeline (string. isinterned (S3 )?? "Null"); // null
Console. writeline (string. isinterned (S1 )?? "Null"); // ABC123
Console. writeline (string. isinterned (s6 )?? "Null"); // ABC123