The common language runtime automatically maintains a table, called the intern pool, which contains a single instance of each unique literal string constant declared in a program, as well as any unique instance of String you add programmatically by calling the Intern method.
The intern pool conserves string storage. If you assign a literal string constant to several variables, each variable is set to reference the same constant in the intern pool instead of referencing several different instances of String that have identical values.
This method looks up str in the intern pool. If str has already been interned, a reference to that instance is returned; otherwise, null is returned.
Compare this method to the Intern method.
This method does not return a Boolean value. If you call the method because you want a Boolean value that indicates whether a particular string is interned, you can use code such as the following.
The intern pool conserves string storage. If you assign a literal string constant to several variables, each variable is set to reference the same constant in the intern pool instead of referencing several different instances of String that have identical values.
This method looks up str in the intern pool. If str has already been interned, a reference to that instance is returned; otherwise, null is returned.
Compare this method to the Intern method.
This method does not return a Boolean value. If you call the method because you want a Boolean value that indicates whether a particular string is interned, you can use code such as the following.
string str1 = "test";
//
string str2 = str1 + "1
test 1"; //
string str3 = str2 + "2
test 2"; //
string[] strings = { "value",
"part1" + "_"
+ "part2", str3,
String.Empty,
null };
foreach (var value in strings)
{
if (value == null)
continue;
bool interned = String.IsInterned(value)
!= null;
if (interned)
Console.WriteLine("{0} is in the string intern pool.",
value);
else
Console.WriteLine("{0} is not in the string intern pool.",
value);
}
// ----------------
// Output is
// First & Second array will be interned ('Value' ,
'part1_part2')
//but str3 is not a interned value.
String str = "abcd";
String
isInterned = String.IsInterned(str);
if (isInterned == null)
Console.WriteLine("{0}', is not interned.", str);
else
Console.WriteLine("{0}', is interned.", isInterned);
//abcd is interned
str = new StringBuilder().Append("AD").Append("BC").ToString();
isInterned = String.IsInterned(str);
if (isInterned == null)
Console.WriteLine("{0}', is not interned.", str);
else
Console.WriteLine("{0}', is interned.", isInterned);
// here ABCD is no an interned
Console.Read();
Happy doing.....
Happy doing.....
No comments:
Post a Comment