Interning of String in C#


Interning of string optimizes memory and performance.

Through this, you can put strings in the runtime's shared string pool.

Let us see an example −

Example

 Live Demo

using System;
using System.Text;

public class Demo {
   public static void Main() {
      string str1 = new StringBuilder().Append("Car is a ").Append("Vehicle").ToString();
      // Interned string
      string str2 = string.Intern(str1);
      Console.WriteLine("Interned String"+str2);
   }
}

Output

Interned StringCar is a Vehicle

Here, we can see Interned string −

string str2 = string.Intern(str1);

Updated on: 20-Jun-2020

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements