英語で読む

次の方法で共有


String.IsInterned(String) メソッド

定義

指定した String への参照を取得します。

C#
public static string? IsInterned(string str);
C#
public static string IsInterned(string str);

パラメーター

str
String

インターン プールから検索する文字列。

戻り値

str が共通言語ランタイムのインターン プール内にある場合は、それへの参照。それ以外の場合は null

例外

strnullです。

次の例は、リテラル文字列がコンパイラによって自動的にインターンされることを示しています。

C#
// Sample for String.IsInterned(String)
using System;
using System.Text;
using System.Runtime.CompilerServices;

// In the .NET Framework 2.0 the following attribute declaration allows you to
// avoid the use of the interning when you use NGEN.exe to compile an assembly
// to the native image cache.
[assembly: CompilationRelaxations(CompilationRelaxations.NoStringInterning)]
class Sample
{
    public static void Main()
    {
        // String str1 is known at compile time, and is automatically interned.
        String str1 = "abcd";

        // Constructed string, str2, is not explicitly or automatically interned.
        String str2 = new StringBuilder().Append("wx").Append("yz").ToString();
        Console.WriteLine();
        Test(1, str1);
        Test(2, str2);
    }

    public static void Test(int sequence, String str)
    {
        Console.Write("{0}) The string, '", sequence);
        String strInterned = String.IsInterned(str);
        if (strInterned == null)
            Console.WriteLine("{0}', is not interned.", str);
        else
            Console.WriteLine("{0}', is interned.", strInterned);
    }
}

//This example produces the following results:

//1) The string, 'abcd', is interned.
//2) The string, 'wxyz', is not interned.

//If you use NGEN.exe to compile the assembly to the native image cache, this
//example produces the following results:

//1) The string, 'abcd', is not interned.
//2) The string, 'wxyz', is not interned.

注釈

共通言語ランタイムは、インターン プールと呼ばれるテーブルを自動的に保持します。このテーブルには、プログラムで宣言された各一意のリテラル文字列定数の 1 つのインスタンスと、 メソッドを呼び出してプログラムによって追加した一意の String インスタンスが Intern 含まれます。

インターン プールは文字列ストレージを節約します。 リテラル文字列定数を複数の変数に割り当てると、各変数は、同じ値を持つ の複数の異なるインスタンス String を参照する代わりに、インターン プール内の同じ定数を参照するように設定されます。

このメソッドは、インターン プールで検索 str します。 既にインターンが行われている場合 str は、そのインスタンスへの参照が返されます。それ以外の場合は が null 返されます。

このメソッドを メソッドと比較します Intern

このメソッドはブール値を返しません。 特定の文字列がインターンされているかどうかを示すブール値が必要であるために メソッドを呼び出す場合は、次のようなコードを使用できます。

C#
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 example displays the following output:
//       'value' is in the string intern pool.
//       'part1_part2' is in the string intern pool.
//       'abc' is not in the string intern pool.
//       '' is in the string intern pool.

注意

Ngen.exe (ネイティブ イメージ ジェネレーター) を使用してローカル コンピューター上のネイティブ イメージ キャッシュにアセンブリをインストールする場合は、インターン プールの使用をオーバーライドできます。 詳細については、 プロパティの「解説」セクションの「パフォーマンスに関 Intern する考慮事項」を参照してください。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

こちらもご覧ください