String.IsInterned(String) 메서드

정의

지정된 String에 대한 참조를 검색합니다.

public:
 static System::String ^ IsInterned(System::String ^ str);
public static string? IsInterned (string str);
public static string IsInterned (string str);
static member IsInterned : string -> string
Public Shared Function IsInterned (str As String) As String

매개 변수

str
String

내부 풀에서 검색할 문자열입니다.

반환

str이 공용 언어 런타임 내부 풀에 있으면 해당 참조이고, 그렇지 않으면 null입니다.

예외

str이(가) null인 경우

예제

다음 예제에서는 리터럴 문자열이 컴파일러에 의해 자동으로 인턴되는 것을 보여 줍니다.

// Sample for String::IsInterned(String)
using namespace System;
using namespace System::Text;
using namespace 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)];
void Test( int sequence, String^ str )
{
   Console::Write( "{0} The string '", sequence );
   String^ strInterned = String::IsInterned( str );
   if ( strInterned == nullptr )
      Console::WriteLine( "{0}' is not interned.", str );
   else
      Console::WriteLine( "{0}' is interned.", strInterned );
}

int 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 = (gcnew StringBuilder)->Append( "wx" )->Append( "yz" )->ToString();
   Console::WriteLine();
   Test( 1, str1 );
   Test( 2, str2 );
}

//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.
// 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.
// Sample for String.IsInterned(String)
open System
open System.Text
open 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)>]
do ()

let test sequence str =
    printf $"%d{sequence}) The string, '"
    let strInterned = String.IsInterned str
    if isNull strInterned then
        printfn $"{str}', is not interned."
    else
        printfn $"{strInterned}', is interned."

// String str1 is known at compile time, and is automatically interned.
let str1 = "abcd"

// Constructed string, str2, is not explicitly or automatically interned.
let str2 = StringBuilder().Append("wx").Append("yz").ToString()
printfn ""
test 1 str1
test 2 str2


//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.
' Sample for String.IsInterned(String)
Imports System.Text
Imports 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 Shared Sub Main()
        ' String str1 is known at compile time, and is automatically interned.
        Dim str1 As [String] = "abcd"

        ' Constructed string, str2, is not explicitly or automatically interned.
        Dim str2 As [String] = New StringBuilder().Append("wx").Append("yz").ToString()
        Console.WriteLine()
        Test(1, str1)
        Test(2, str2)
    End Sub

    Public Shared Sub Test(ByVal sequence As Integer, ByVal str As [String])
        Console.Write("{0}) The string, '", sequence)
        Dim strInterned As [String] = [String].IsInterned(str)
        If strInterned Is Nothing Then
            Console.WriteLine("{0}', is not interned.", str)
        Else
            Console.WriteLine("{0}', is interned.", strInterned)
        End If
    End Sub
End Class

'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.

설명

공용 언어 런타임은 프로그램에 선언된 각 고유 리터럴 문자열 상수의 단일 인스턴스와 메서드를 호출 Intern 하여 프로그래밍 방식으로 추가하는 고유한 인스턴스 String 를 포함하는 인턴 풀이라는 테이블을 자동으로 유지 관리합니다.

인턴 풀은 문자열 스토리지를 절약합니다. 여러 변수에 리터럴 문자열 상수를 할당하는 경우 각 변수는 동일한 값을 가진 의 여러 인스턴스를 참조하는 대신 인턴 풀에서 동일한 상수를 참조하도록 설정됩니다 String .

이 메서드는 인턴 풀에서 조회 str 합니다. 가 이미 인턴된 경우 str 해당 인스턴스에 대한 참조가 반환되고, null 그렇지 않으면 가 반환됩니다.

이 메서드를 메서드와 비교합니다 Intern .

이 메서드는 부울 값을 반환하지 않습니다. 특정 문자열이 인턴되는지 여부를 나타내는 부울 값을 원하기 때문에 메서드를 호출하는 경우 다음과 같은 코드를 사용할 수 있습니다.

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.
open System

let str1 = "a"
let str2 = str1 + "b"
let str3 = str2 + "c"
let strings = 
    [| "value"; "part1" + "_" + "part2"; str3
       String.Empty; null |]
for value in strings do
    if value <> null then
        let interned = String.IsInterned(value) <> null
        if interned then
            printfn $"'{value}' is in the string intern pool."
        else
            printfn $"'{value}' is not in the string intern pool."
// 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.
Module Example
   Public Sub Main()
      Dim str1 As String = "a"
      Dim str2 As String = str1 + "b"
      Dim str3 As String = str2 + "c"
      Dim strings() As String = { "value", "part1" + "_" + "part2", str3, 
                                  String.Empty, Nothing }
      For Each value In strings
         If value Is Nothing Then Continue For
         
         Dim interned As Boolean = (String.IsInterned(value) IsNot Nothing)
         If interned Then
            Console.WriteLine("'{0}' is in the string intern pool.", 
                              value)
         Else
            Console.WriteLine("'{0}' is not in the string intern pool.",
                              value)                      
         End If
      Next
   End Sub
End Module
' 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 주의 섹션의 성능 고려 사항을 참조하세요.

적용 대상

추가 정보