String.Intern メソッドとは? わかりやすく解説

String.Intern メソッド

指定した String へのシステム参照取得します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ArgumentNullException

strnull 参照 (Visual Basic では Nothing) です。

解説解説

共通言語ランタイムは、インターン プール呼ばれるテーブル保持することで文字列ストレージ管理してます。このテーブルには、プログラム内で宣言または作成され一意リテラル文字列対す単一参照含まれています。この結果として特定の値を持つリテラル文字列インスタンスは、システム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 では、str1str2等価でないと見なされるのに対し.NET Framework Version 2.0 では str1str2等価であると見なされます

パフォーマンスに関する考慮事項
使用例使用例

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
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

String.Intern メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



String.Intern メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS