C# String IsInterned

Description

String IsInterned retrieves a reference to a specified String.

Syntax

String.IsInterned has the following syntax.


public static string IsInterned(
  string str
)

Parameters

String.IsInterned has the following parameters.

  • str - The string to search for in the intern pool.

Returns

String.IsInterned method returns A reference to str if it is in the common language runtime intern pool; otherwise, null.

Example

To get a Boolean value that indicates whether a particular string is interned, you can use code such as the following.


//from w w  w .  jav  a  2  s.  c  o m
using System;

public class Example
{
   public static void Main()
   {
      string str1 = "a";
      string str2 = str1 + "b";
      string str3 = str2 + "c";
      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);                      
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version