String.IsInterned(String) 方法

定義

擷取對指定 String 的參考。

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

參數

str
String

要在保留集區中搜尋的字串。

傳回

如果是在 Common Language Runtime 保留集區中,則為 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.

備註

Common Language Runtime 會自動維護稱為「intern 集區」的資料表,其中包含程式中宣告的每個唯一常值字串常數的單一實例,以及透過呼叫 Intern 方法來以程式設計方式新增的任何唯一實例 String

Intern 集區會節省字串儲存體。 如果您將常值字串常數指派給數個變數,則每個變數都會設定為參考整數集區中的相同常數,而不是參考具有相同值的數個不同實例 String

這個方法會在tern 集區中查閱 str 。 如果 str 已經記憶體,則會傳回該實例的參考, null 否則會傳回。

比較這個方法與 Intern 方法。

這個方法不會傳回布林值。 如果您因為想要布林值來指出特定字串是否為terned,所以呼叫 方法時,可以使用下列程式碼。

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 (Native Image Generator) ,將元件安裝到本機電腦上的原生映射快取時,您可以覆寫內部集區的用法。 如需詳細資訊,請參閱 屬性一節 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

另請參閱