
A table of internal strings is maintained. In the .NET Framework, this enables the sharing of string data for equivalent strings. With the string.IsInterned method, you can test whether a string is present in the internal table, without adding it if it is not.
This C# example program shows the string.IsInterned method. IsInterned returns an interned string if one exists.

To begin, the string.IsInterned method receives a string reference and returns a string reference. If the internal string is found, that reference is returned; if no internal string is present, a null reference is returned. In the first part of this example, a string literal is tested, and this reveals that string literals are always present in the intern table. On the other hand, a dynamically constructed string is not present in the intern table, and IsInterned will return null in this case.
Program that uses IsInterned method [C#]
using System;
class Program
{
static void Main()
{
// See if a string literal is interned.
string value1 = "cat";
string value2 = string.IsInterned(value1);
Console.WriteLine(value2);
// See if a dynamically constructed string is interned.
string value3 = "cat" + 1.ToString();
string value4 = string.IsInterned(value3);
Console.WriteLine(value4 == null);
}
}
Output
cat
True
In most programs, you do not need to use the string.Intern or string.IsInterned methods. However, if you have a program that uses the intern table in other places, you might want to use the string.IsInterned method for string data that is possibly in the intern table, but that is not important enough to add if it is not already present. In other words, the string.IsInterned method returns references to already existing data, but does not add anything.

The string.IsInterned method is one of the least-used ones on the string type, and this is for a good reason: it has a very limited purpose. Most C# programs do not use the intern table extensively, as there is little point to this. In some programs, though, the string.IsInterned method can be used to reduce memory usage for string data.
Intern String Type