英語で読む

次の方法で共有


String.Intern(String) メソッド

定義

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

public static string Intern(string str);

パラメーター

str
String

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

戻り値

str がインターン プールに存在する場合は、それに対するシステム参照。それ以外の場合は、値が str の文字列への新しい参照。

例外

strnullです。

次の例では、値が等しい 3 つの文字列を使用して、新しく作成された文字列とインターン文字列が等しいかどうかを判断します。

// 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
*/

注釈

この API の詳細については、「 String.Intern の補足 API 解説」を参照してください。

適用対象

製品 バージョン
.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

こちらもご覧ください