C# String Intern

Description

String Intern retrieves the system's reference to the specified String.

Syntax

String.Intern has the following syntax.


public static string Intern(
  string str
)

Parameters

String.Intern has the following parameters.

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

Returns

String.Intern method returns The system's reference to str, if it is interned; otherwise, a new reference to a string with the value of str.

Example

Sample for String.Intern(String)


/*ww w.  j ava  2s.c om*/
using System;
using System.Text;

class Sample {
    public static void Main() {
        String s1 = "MyTest";
        String s2 = new StringBuilder().Append("My").Append("Test").ToString(); 
        String s3 = String.Intern(s2); 
        Console.WriteLine("s1 == '{0}'", s1);
        Console.WriteLine("s2 == '{0}'", s2);
        Console.WriteLine("s3 == '{0}'", s3);
        Console.WriteLine("Is s2 the same reference as s1?: {0}", (Object)s2==(Object)s1); 
        Console.WriteLine("Is s3 the same reference as s1?: {0}", (Object)s3==(Object)s1);
    }
}

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