String.
Intern(String)
String. Intern(String)
String. Intern(String)
String. Intern(String)
Method
Definition
public: static System::String ^ Intern(System::String ^ str);
public static string Intern (string str);
static member Intern : string -> string
Public Shared Function Intern (str As String) As String
Parameters
Returns
The system's reference to
str, if it is interned; otherwise, a new reference to a string with the value of
str.
Exceptions
str is
null.
Examples
The following example uses three strings that are equal in value to determine whether a newly created string and an interned string are equal.
// Sample for String::Intern(String) using namespace System; using namespace System::Text; int main() { String^ s1 = "MyTest"; String^ s2 = (gcnew}", s2 == s1 ); Console::WriteLine( "Is s3 the same reference as s1?: {0}", s3 == s1 ); } /* This example produces the following results: s1 == 'MyTest' s2 == 'MyTest' s3 == 'MyTest' Is s2 the same reference as s1?: False Is s3 the same reference as s1?: True */
// Sample for String.Intern(String) 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 == {s1}"); Console.WriteLine($"s2 == {s2}"); Console.WriteLine($"s3 == {s3}"); Console.WriteLine($"Is s2 the same reference as s1?: {(Object)s2 == (Object)s1}"); Console.WriteLine($"Is s3 the same reference as s1?: {(Object)s3 == (Object)s1}"); } } /* This example produces the following results: s1 == MyTest s2 == MyTest s3 == MyTest Is s2 the same reference as s1?: False Is s3 the same reference as s1?: True */
Imports System.Text Class Sample Public Shared Sub Main() Dim s1 As String = "MyTest" Dim s2 As String = New StringBuilder().Append("My").Append("Test").ToString() Dim s3 As String = String.Intern(s2) Console.WriteLine($"s1 = {s1}") Console.WriteLine($"s2 = {s2}") Console.WriteLine($"s3 = {s3}") Console.WriteLine($"Is s2 the same reference as s1?: {s2 Is s1}") Console.WriteLine($"Is s3 the same reference as s1?: {s3 Is s1}") End Sub End Class ' 's1 = MyTest 's2 = MyTest 's3 = MyTest 'Is s2 the same reference as s1?: False 'Is s3 the same reference as s1?: True '
Remarks.
string s1 = "MyTest"; string s2 = new StringBuilder().Append("My").Append("Test").ToString(); string s3 = String.Intern(s2); Console.WriteLine((Object)s2==(Object)s1); // Different references. Console.WriteLine((Object)s3==(Object)s1); // The same reference.
Dim s1 As String = "MyTest" Dim s2 As String = New StringBuilder().Append("My").Append("Test").ToString() Dim s3 As String = String.Intern(s2) Console.WriteLine(CObj(s2) Is CObj(s1)) ' Different references. Console.WriteLine(CObj(s3) Is CObj(s1)) ' The same reference.
Compare this method to the IsInterned method.
Version Considerations
In the .NET Framework 3.5 Service Pack 1, the Intern method reverts to its behavior in the .NET Framework 1.0 and.
string str1 = String.Empty; string str2 = String.Empty; StringBuilder sb = new StringBuilder().Append(String.Empty); str2 = String.Intern(sb.ToString()); if((object)str1==(object)str2) Console.WriteLine("The strings are equal."); else Console.WriteLine("The strings are not equal.");
Dim str1 As String = String.Empty Dim str2 As String = String.Empty Dim sb As StringBuilder = New StringBuilder().Append(String.Empty) str2 = String.Intern(sb.ToString()) If CObj(str1) Is CObj(str2) Then Console.WriteLine("The strings are equal.") Else Console.WriteLine("The strings are not equal.") End If
In the .NET Framework 1.0, .NET Framework 1.1, and .NET Framework 3.5 SP1,
str1 and
str2 are equal. In the .NET Framework 2.0 Service Pack 1 and .NET Framework to version.