String.Intern メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim str As String Dim returnValue As String returnValue = String.Intern(str)
戻り値
str の値が既にインターン プールに存在する場合は、システムの参照が返されます。それ以外の場合は、str の値を持つ文字列への新しい参照が返されます。


共通言語ランタイムは、インターン プールと呼ばれるテーブルを保持することで文字列のストレージを管理しています。このテーブルには、プログラム内で宣言または作成された一意のリテラル文字列に対する単一の参照が含まれています。この結果として、特定の値を持つリテラル文字列のインスタンスは、システムに 1 つしか存在しません。
たとえば、いくつかの変数に同じリテラル文字列を代入した場合、ランタイムはそのリテラル文字列に対する同じ参照をインターン プールから取得して、それぞれの変数に代入します。
Intern メソッドは、インターン プールを使用して、str の値と等しい文字列を検索します。そのような文字列が存在する場合は、インターン プール内の該当する参照が返されます。文字列が存在しない場合は、str への参照がインターン プールに追加された後、その参照が返されます。
次の C# の例では、"MyTest" という値を持つ文字列 s1 は、プログラム内のリテラルであるため、既にインターン プールに存在します。
System.Text.StringBuilder クラスは、s1 と同じ値を持つ新しい文字列オブジェクトを生成します。その文字列への参照が s2 に代入されます。
Intern メソッドは、s2 と同じ値を持つ文字列を検索します。検索する文字列が存在するため、このメソッドは s1 に代入されているものと同じ参照を返します。次に、その参照が s3 に代入されます。
参照 s1 と参照 s2 は別のオブジェクトを参照しているため、等しくないものと判定されます。これに対して、参照 s1 と参照 s3 は同じオブジェクトを参照しているため、等しいものと判定されます。
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.
このメソッドと IsInterned メソッドを比較します。
バージョンの考慮事項.NET Framework Version 2.0 以降では、Intern メソッドの動作が変更されています。次の C# コードでは、変数 str1 には Empty への参照を代入し、変数 str2 には、Intern メソッドによって返される Empty への参照を代入しています。その後、str1 に格納された参照と str2 に格納された参照とを比較し、等しいかどうかを確認します。
string str1 = String.Empty; string str2 = String.Intern(String.Empty); if ((object) str1) == ((object) str2) …
.NET Framework Version 1.1 では、str1 と str2 が等価でないと見なされるのに対し、.NET Framework Version 2.0 では str1 と str2 は等価であると見なされます。
アプリケーションによって確保されるメモリの全体的な使用量を抑えるという観点で見た場合、文字列への参照をインターン プールに格納すると、思わぬ副作用が生じることに注意してください。第一に、String オブジェクト用にインターン プールに確保されるメモリは、共通言語ランタイム (CLR) が終了するまで解放されない可能性があります。これは、CLR は、インターン プールに格納された String オブジェクトを、アプリケーション (またはアプリケーション ドメイン) の終了後も参照し続ける場合があるためです。第二に、文字列の参照をインターン プールに格納した場合、最初に文字列を作成する必要があります。String オブジェクトによって使用されたメモリは、最終的にガベージ コレクタによって解放されるまで割り当てられたままになります。

3 つの同じ文字列を使用して、新たに作成した文字列と、インターン プールに格納された文字列の参照とが等価であるかどうかを確認するコード例を次に示します。
' Sample for String.Intern(String) Imports System 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 = '{0}'", s1) Console.WriteLine("s2 = '{0}'", s2) Console.WriteLine("s3 = '{0}'", s3) Console.WriteLine("Is s2 the same reference as s1?: {0}", s2 Is s1) Console.WriteLine("Is s3 the same reference as s1?: {0}", s3 Is s1) End Sub 'Main End Class 'Sample ' '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 == '{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); } } /* 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 namespace System; using namespace System::Text; int main() { String^ s1 = "MyTest"; String^ s2 = (gcnew 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}", 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) import System.*; import System.Text.*; class Sample { public static void main(String[] args) { 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}", System.Convert.ToString((Object)s2 == (Object)s1)); Console.WriteLine("Is s3 the same reference as s1?: {0}", System.Convert.ToString((Object)s3 == (Object)s1)); } //main } //Sample /* 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 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からString.Intern メソッドを検索する場合は、下記のリンクをクリックしてください。

- String.Intern メソッドのページへのリンク