String Interning

Review of String interning

Jeffrey Richter's Applied Microsoft .Net Framework Programming p262-266

    1 using System;

    2  

    3 namespace StringInterning

    4 {

    5     class TestHarness

    6     {

    7         [STAThread]

    8         static void Main(string[] args)

    9         {

   10             string s = "Hello world";

   11             Console.WriteLine(Object.ReferenceEquals("Hello world", s)); // true

   12  

   13             string s1 = "Hello";

   14             string s2 = "Hel";

   15             string s3 = s2 + "lo";

   16  

   17             Console.WriteLine(Object.ReferenceEquals(s1, s3)); // false

   18             Console.WriteLine(s1.Equals(s3)); // true

   19  

   20             s3 = String.Intern(s3);

   21             Console.WriteLine(Object.ReferenceEquals(s1, s3)); // true

   22             Console.WriteLine(s1.Equals(s3)); // true

   23         }

   24     }

   25 }

 

posted on Sunday, January 23, 2005 1:47 PM

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Url
Comment   
Please enter the following code into the box below to stop spammers

  
Enter Code Here *