Lire en anglais

Partager via


String.IsInterned(String) Méthode

Définition

Récupère une référence à un String spécifié.

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

Paramètres

str
String

Chaîne à rechercher dans le pool interne.

Retours

Référence à str si elle figure dans le pool interne du Common Language Runtime ; sinon, null.

Exceptions

str a la valeur null.

Exemples

L’exemple suivant montre que les chaînes littérales sont internement automatiquement par le compilateur.

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.

Remarques

Le Common Language Runtime gère automatiquement une table, appelée pool interne, qui contient une seule instance de chaque constante de chaîne littérale unique déclarée dans un programme, ainsi que toute instance unique que String vous ajoutez par programmation en appelant la Intern méthode .

Le pool interne conserve le stockage de chaînes. Si vous affectez une constante de chaîne littérale à plusieurs variables, chaque variable est définie pour référencer la même constante dans le pool interne au lieu de référencer plusieurs instances différentes de qui ont des String valeurs identiques.

Cette méthode recherche str dans le pool de stagiaires. Si str a déjà été interne, une référence à cette instance est retournée ; sinon, null est retournée.

Comparez cette méthode à la Intern méthode .

Cette méthode ne retourne pas de valeur booléenne. Si vous appelez la méthode parce que vous souhaitez une valeur booléenne qui indique si une chaîne particulière est interne, vous pouvez utiliser du code tel que le suivant.

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.

Notes

Vous pouvez remplacer l’utilisation du pool interne lorsque vous utilisez Ngen.exe (Générateur d’images natives) pour installer un assembly dans le cache d’images natives sur un ordinateur local. Pour plus d’informations, consultez Considérations relatives aux performances dans la section Notes de la Intern propriété .

S’applique à

Produit Versions
.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

Voir aussi