String Interning

by Jason Haley 23. January 2005 08:47

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 }

 

Comments (0) | Post RSSRSS comment feed |

Categories:
Tags:

Comments are closed