Leggere in inglese

Condividi tramite


String.Intern(String) Metodo

Definizione

Recupera il riferimento del sistema all'oggetto String specificato.

C#
public static string Intern(string str);

Parametri

str
String

Stringa da cercare nel pool di centralizzazione.

Restituisce

Riferimento del sistema a str, se è centralizzato; in caso contrario, nuovo riferimento a una stringa con il valore di str.

Eccezioni

str è null.

Esempio

Nell'esempio seguente vengono usate tre stringhe uguali in valore per determinare se una stringa appena creata e una stringa internata sono uguali.

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

Commenti

Per altre informazioni su questa API, vedere Osservazioni api supplementari per String.Intern.

Si applica a

Prodotto Versioni
.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

Vedi anche