Aracılığıyla paylaş


String.IsInterned(String) Yöntem

Tanım

Belirtilen Stringbir öğesine başvuru alır.

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

Parametreler

str
String

Stajyer havuzunda aranacak dize.

Döndürülenler

Ortak dil çalışma zamanı stajyer havuzunda olup olmadığını içeren bir başvuru str ; aksi takdirde, null.

Özel durumlar

str, null'e eşittir.

Örnekler

Aşağıdaki örnek, dizelerin stajyer havuzunda olup olmadığını denetlemek ve stajyer dizelerin başvuru eşitliğini doğrulamak için nasıl kullanılacağını IsInterned gösterir.

// Sample for String.IsInterned(String)
using System;
using System.Text;

class Sample
{
    public static void Main()
    {
        // Constructed strings are not automatically interned.
        string s1 = new StringBuilder().Append("My").Append("Test").ToString();
        string s2 = new StringBuilder().Append("My").Append("Test").ToString();

        // Neither string is in the intern pool yet.
        Console.WriteLine($"Is s1 interned? {String.IsInterned(s1) != null}");
        Console.WriteLine($"Is s2 interned? {String.IsInterned(s2) != null}");

        // Intern s1 explicitly.
        string i1 = String.Intern(s1);

        // Now s2 can be found in the intern pool.
        string i2 = String.IsInterned(s2);

        Console.WriteLine($"Is s2 interned after interning s1? {i2 != null}");
        Console.WriteLine($"Are i1 and i2 the same reference? {Object.ReferenceEquals(i1, i2)}");
    }
}

// This example produces the following results:
//
// Is s1 interned? False
// Is s2 interned? False
// Is s2 interned after interning s1? True
// Are i1 and i2 the same reference? True
// Sample for String.IsInterned(String)
open System
open System.Text

// Constructed strings are not automatically interned.
let s1 = StringBuilder().Append("My").Append("Test").ToString()
let s2 = StringBuilder().Append("My").Append("Test").ToString()

// Neither string is in the intern pool yet.
printfn $"Is s1 interned? {String.IsInterned(s1) <> null}"
printfn $"Is s2 interned? {String.IsInterned(s2) <> null}"

// Intern s1 explicitly.
let i1 = String.Intern(s1)

// Now s2 can be found in the intern pool.
let i2 = String.IsInterned(s2)

printfn $"Is s2 interned after interning s1? {i2 <> null}"
printfn $"Are i1 and i2 the same reference? {Object.ReferenceEquals(i1, i2)}"

// This example produces the following results:
//
// Is s1 interned? False
// Is s2 interned? False
// Is s2 interned after interning s1? True
// Are i1 and i2 the same reference? True

' Constructed strings are not automatically interned.
Dim s1 As String = New StringBuilder().Append("My").Append("Test").ToString()
Dim s2 As String = New StringBuilder().Append("My").Append("Test").ToString()

' Neither string is in the intern pool yet.
Console.WriteLine($"Is s1 interned? {String.IsInterned(s1) IsNot Nothing}")
Console.WriteLine($"Is s2 interned? {String.IsInterned(s2) IsNot Nothing}")

' Intern s1 explicitly.
Dim i1 As String = String.Intern(s1)

' Now s2 can be found in the intern pool.
Dim i2 As String = String.IsInterned(s2)

Console.WriteLine($"Is s2 interned after interning s1? {i2 IsNot Nothing}")
Console.WriteLine($"Are i1 and i2 the same reference? {Object.ReferenceEquals(i1, i2)}")

' This example produces the following results:
'
' Is s1 interned? False
' Is s2 interned? False
' Is s2 interned after interning s1? True
' Are i1 and i2 the same reference? True

Açıklamalar

Ortak dil çalışma zamanı, bir programda bildirilen her benzersiz değişmez dize sabitinin tek bir örneğini içeren ve yöntemini çağırarak String program aracılığıyla eklediğiniz herhangi bir benzersiz örneği Intern içeren, intern havuzu olarak adlandırılan bir tabloyu otomatik olarak tutar.

Stajyer havuzu dize depolamasını korur. Çeşitli değişkenlere değişmez dize sabiti atarsanız, her değişken aynı değerlere sahip olan birkaç farklı örneğine başvurmak yerine stajyer havuzunda aynı sabite String başvuracak şekilde ayarlanır.

Bu yöntem stajyer havuzunda arar str . str Zaten interned ise, bu örneğe bir başvuru döndürülür; aksi takdirde döndürülürnull.

Bu yöntemi yöntemiyle karşılaştırın Intern .

Bu yöntem Boole değeri döndürmez. Belirli bir dizenin interned olup olmadığını gösteren bir Boole değeri istediğiniz için yöntemini çağırırsanız, aşağıdaki gibi bir kod kullanabilirsiniz.

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.

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($"'{value}' is in the string intern pool.")
    Else
        Console.WriteLine($"'{value}' is not in the string intern pool.")
    End If
Next

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

Şunlara uygulanır

Ayrıca bkz.