String.IsInterned 方法

检索对指定 String 的引用。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Shared Function IsInterned ( _
    str As String _
) As String
用法
Dim str As String
Dim returnValue As String

returnValue = String.IsInterned(str)
public static string IsInterned (
    string str
)
public:
static String^ IsInterned (
    String^ str
)
public static String IsInterned (
    String str
)
public static function IsInterned (
    str : String
) : String

参数

返回值

如果 str 位于公共语言运行库“拘留池”中,则为对它的 String 引用;否则为 空引用(在 Visual Basic 中为 Nothing)。

异常

异常类型 条件

ArgumentNullException

str 为 空引用(在 Visual Basic 中为 Nothing)。

备注

公共语言运行库会自动维护一个名为“拘留池”(intern pool) 的表,它包含在程序中声明的每个唯一字符串常数的单个实例,以及以编程方式添加的 String 的任何唯一实例。

该拘留池节约字符串存储区。如果将字符串常数分配给几个变量,则每个变量设置为引用“拘留池”(intern pool) 中的同一常数,而不是引用具有相同值的 String 的几个不同实例。

此方法在拘留池中查找 str。如果已经将 str 放入拘留池中,则返回对此实例的引用;否则返回 空引用(在 Visual Basic 中为 Nothing)。

将此方法与 Intern 方法进行比较。

此方法不返回布尔值,但仍可以在需要布尔值的地方使用。

示例

下面的代码示例演示文本字符串自动由编译器留用的过程。

' Sample for String.IsInterned(String)
Imports System
Imports System.Text

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 'Main
   
   Public Shared Sub Test(sequence As Integer, 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 'Test
End Class 'Sample '
'This example produces the following results:
'
'1) The string, 'abcd', is interned.
'2) The string, 'wxyz', is not interned.
'
// Sample for String.IsInterned(String)
using System;
using System.Text;

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.
*/
// Sample for String::IsInterned(String)
using namespace System;
using namespace System::Text;
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.
*/
// Sample for String.IsInterned(String)
import System.*;
import System.Text.*;

class Sample
{
    public static void main(String[] args)
    {
        // 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);
    } //main

    public static void Test(int sequence, String str)
    {
        Console.Write("{0}) The string, '", System.Convert.ToString(sequence));
        String strInterned = String.IsInterned(str);
        if (strInterned == null) {
            Console.WriteLine("{0}', is not interned.", str);
        }
        else {
            Console.WriteLine("{0}', is interned.", strInterned);
        }
    } //Test
} //Sample 
 /*
This example produces the following results:

1) The string, 'abcd', is interned.
2) The string, 'wxyz', is not interned.
*/

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

String 类
String 成员
System 命名空间
Intern